JavaScript Flashcards
What is the purpose of variables?
to store data values that can be used later
How do you declare a variable?
var variableName;
How do you initialize (assign a value to) a variable?
variableName = value;
What characters are allowed in variable names?
letters, underscore, dollar sign, numbers (but variable name cannot start with a number)
What does it mean to say that variable names are “case sensitive”?
variable names with different casing constitute different variables
What is the purpose of a string?
to store words, letters, and characters
What is the purpose of a number?
to store numeric values
What is the purpose of a boolean?
to store the value of true/false
What does the = operator mean in JavaScript?
assigns a value to a variable
How do you update the value of a variable?
= assignment operator
What is the difference between null and undefined?
null must be intentionally assigned to a variable, while undefined is automatically assigned when a variable was not assigned a value
Why is it a good habit to include “labels” when you log values to the browser console?
to make clear which variables are being logged and in what order
Give five examples of JavaScript primitives.
number, string, boolean, null, undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
combines strings
What purpose(s) does the + plus operator serve in JavaScript?
addition for numbers and concatenation for strings
What data type is returned by comparing two values ( < , > , === , etc)?
boolean
What does the += “plus-equals” operator do?
adds the value of the operand to the variable and assigns the result to the variable
What are objects used for?
group together related variables and functions
What are object properties?
variables that are part of an object which have a unique key name and correlated value
Describe object literal notation.
object in curly braces { } each key separated from its value using a colon, each key/value pair separated by commas
How do you remove a property from an object?
delete objectName.propertyName;
What are the two ways to get or update the value of a property?
objectName.propertyName (dot notation) or objectName[“propertName”] (bracket notation)
What are arrays used for?
storing a list of values
Describe array literal notation.
square brackets [ ] each value is separated by a comma
How are arrays different from “plain” objects?
arrays are ordered and the keys are the index numbers (rather than the property names)
What number represents the first index of an array?
0
What is the length property of an array?
number of items in the array
How do you calculate the last index of an array?
arrayName.length - 1
What is a function in JavaScript?
a block of code designed to perform a particular task
Describe the parts of a function definition.
function keyword, (optional) function name, comma-separated list of 0+ parameters surrounded by parentheses, curly braces, (optional) return statement
Describe the parts of a function call.
function name + parentheses with arguments (if function definition has them)
When comparing them side-by-side, what are the differences between a function call and a function definition?
the definition has the function keyword and the code block with curly braces
What is the difference between a parameter and an argument?
parameters are placeholders in the function definition; arguments are passed to the function when it is called
Why are function parameters useful?
way to provide data to get different results from functions
What two effects does a return statement have on the behavior of a function?
1) causes the function to produce a value; 2) exits the function
Why do we log things to the console?
to debug
What is a method?
a function that is a property of an object
How is a method different from any other function?
it is called on an object
How do you remove the last element from an array?
.pop( )
How do you round a number down to the nearest integer?
Math.floor( )
How do you generate a random number?
Math.random( )
How do you delete an element from an array?
.splice( ) or .pop( ) or .shift( )
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?
no; can check by logging the string to the console or read up MDN
Is the return value of a function or method useful in every situation?
not necessarily
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Give 6 examples of comparison operators.
< , <= , > , >= , === , !==
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
conditionally runs a block of code
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword, condition in parentheses, curly braces
What are the three logical operators?
&& , || , !
How do you compare two different expressions in the same condition?
&& or ||
What is the purpose of a loop?
to repeat a set of steps
What is the purpose of a condition expression in a loop?
to determine when the loop should stop
What does “iteration” mean in the context of loops?
the code block being executed once
When does the condition expression of a while loop get evaluated?
each time before the code block is executed
When does the initialization expression of a for loop get evaluated?
once at the beginning of the loop, before the condition
When does the condition expression of a for loop get evaluated?
each time before the code block is executed
When does the final expression of a for loop get evaluated?
each time after the code block is executed
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 1