JavaScript Flashcards
what is the purpose of a variable?
to store information to be referenced to
How do you declare a variable?
variable keyword and variable name
How do you initialize (assign a value to) a variable?
with an = sign (assignment operator) and a variable value after it
var nameOfVar = value (string, number, boolean data type)
What characters are allowed in variable names?
letters, numbers, dollar sign, or underscore
but a number cannot be the first character
What does it mean to say that variable names are “case sensitive”?
when you call the variable name, you have to call it by its exact value or the computer won’t recognize it
What is the purpose of a string?
to store a collection of characters (could be a letter, number, or a backslash)
What is the purpose of a number?
to store numbers
What is the purpose of a boolean?
to determine which code should run?
to store true/false value
What does the = operator mean in JavaScript?
variable assignment
provide variable value to a variable
How do you update the value of a variable?
call the variable name, assignment operator, and the new variable value
What is the difference between null and undefined?
null is an assigned value– it means there is nothing in the variable
undefined means a variable has been declared but not defined with values yet
as a developer, you never want to assign a variable as undefined
Why is it a good habit to include “labels” when you log values to the browser console?
so you can tell which variables are being logged and in what order– this is especially helpful when you need to debug
Give five examples of JavaScript primitives.
Number, String, Boolean, Null, Undefined
What data type is returned by an arithmetic operation?
number
What purpose(s) does the + plus operator serve in JavaScript?
to concatenate strings or add numbers
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds/concatnates value from the right to the left variable
the current value of that variable, bring in some value, add that on to the current value, and the result of that is the new value
What is string concatenation?
the process of joining multiple strings together
What are objects used for?
used to group together a set of variables and functions to create a model
What are object properties?
a variable– properties tell us about the object
Describe object literal notation.
its an array of key-value pairs with a colon separating the keys and values and a comma after every key-value pair (except for the last)
How do you remove a property from an object?
you use the delete keyword followed by the object name and the property name
What are the two ways to get or update the value of a property?
dot notation or bracket notation
What are object methods?
a function– methods represent tasks that are associated with the object
What are arrays used for?
arrays are used to store a list of values that are related to each other
Describe array literal notation.
values are assigned to the array inside a pair of square brackets and each value is separated by a comma
How are arrays different from “plain” objects?
like objects, arrays hold a key value pairs, but with arrays, the key for each value is its index number
array is a list of items and objects are collections of individual items?
objects are a collection of data with individual name of properties with no set order and arrays are in a set order and are a list of items arrays hold data that are similar
What number represents the first index of an array?
0
What is the length property of an array?
the amount of items in the array
How do you calculate the last index of an array?
array length minus 1
What is a function in JavaScript?
it’s essentially a block of code or statement you write that performs a task or calculates a value and it can be used multiple times
Describe the parts of a function definition.
- function keyword
- function name
- parentheses with parameters inside
- curly braces
- inside curly braces, you have the function code block
Describe the parts of a function call.
- name of the function
- open and close parentheses
- inside the parentheses, you have arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
a function definition is a lot longer than a function call with all the tasks it needs to perform, however, a function definition alone is not going to perform any action unless the function call calls the function
What is the difference between a parameter and an argument?
parameter is essentially a placeholder for when we call the function and pass in an argument
Why are function parameters useful?
parameters allow us to pass instructions into a function
What two effects does a return statement have on the behavior of a function?
1) causes the function to produce a value we can use
2) prevents any more code in the function code block from being run
Why do we log things to the console?
it informs what the code is doing and alerts you if there’s an issue– so logging things to the console is great for debugging or when you don’t understand code block
to see what the code we’re writing is doing
What is a method?
a method is a function which is a property of an object
a function stored in a property of an object
How is a method different from any other function?
a method is attached to an object
in order to call a method, you have to attach the function name to their object using dot notation, and to call a regular function, you use its name
How do you remove the last element from an array?
pop() method
How do you round a number down to the nearest integer?
floor() method
How do you generate a random number?
Math.random() (from 0 - .9999)
what’s the purpose?
treat it as a percentage, that will get multiplied with your range value
random acts as a decimal
How do you delete an element from an array?
splice() method
arguments: (index, how many elements to remove)
How do you append an element to an array?
push()
How do you break a string up into an array?
split()
Do string methods change the original string? How would you check if you weren’t sure?
string methods do not change your original string
you can check by logging your value to the console
strings are immutable, you cannot change the original string
Roughly how many string methods are there according to the MDN Web docs?
a lot
Is the return value of a function or method useful in every situation?
no
like the push method, you dont need to know the length of it
Roughly how many array methods are there according to the MDN Web docs?
a lot
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN, official docs, it has all the documents you need
Give 6 examples of comparison operators.
less than, greater than, less than or equal to, greater than or equal to, strictly equal, strictly not equal
What is the purpose of an if statement?
to test conditions, if the condition is true, the subsequent code statement runs
to make decicions
Is else required in order to use an if statement?
no
only use else if you need to
Describe the syntax (structure) of an if statement.
if keyword open and close parenthesis a condition inside the parentheses open and curly braces code to be executed inside the curly braces if condition is true
What are the three logical operators?
logical and operator
logical or operator
logical not operator
How do you compare two different expressions in the same condition?
expression one on one side expression two on the other side and a logical operator in between follow by another pair of parenthesis to wrap the two expressions
by using logical operaters
What data type do comparison expressions evaluate to?
boolean
What is the purpose of a loop?
to repeat similar code a number of times
to repeat code and when to stop repreating code
What is the purpose of a condition expression in a loop?
it lets the loop statement know whether it should be executed
we can think of condition as a break, if its truthy we continue if its falsy we stop
What does “iteration” mean in the context of loops?
repeat?
When does the condition expression of a while loop get evaluated?
first
When does the initialization expression of a for loop get evaluated?
first, before anything, and only once
When does the condition expression of a for loop get evaluated?
second, your condition run betore each iteration
When does the final expression of a for loop get evaluated?
last, after the code block has ran? it is run after the code block
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
What does the ++ increment operator do?
adds one to its operand, increments the variable by one
How do you iterate through the keys of an object?
for in loop