JavaScript Flashcards
What is the purpose of variables?
Let you store data in your program for later use or for modification
How do you declare a variable?
var variableName;
How do you initialize (assign a value to) a variable?
Equals sign
What characters are allowed in variable names?
First character: Letter, _, $
Other characters: Letter, _, $, Number
What does it mean to say that variable names are “case sensitive”?
Capital letters are different from lowercase letters
What is the purpose of a string?
A literal representation of characters (holds letters)
What is the purpose of a number?
Contains numerical data
What is the purpose of a boolean?
True/false logic
What does the = operator mean in JavaScript?
Assigns a value to a variable
How do you update the value of a variable?
Equals sign without var
What is the difference between null and undefined?
Undefined: the variable is declared but doesn’t have a value/hasn’t been assigned
Null indicates the variable has an empty or non-existent value, must be assigned
Why is it a good habit to include “labels” when you log values to the browser console?
Makes it easier and clearer to keep track of your variables
Give five examples of JavaScript primitives.
String, Number, Boolean, Null, Undefined
What is string concatenation?
Combining strings into a new string
What purpose(s) does the + plus operator serve in JavaScript?
Add numbers together or concatenate strings
What data type is returned by comparing two values (, ===, etc)?
Boolean
What does the += “plus-equals” operator do?
Adds the value on the right side to the variable and assigns the result to the variable
What are objects used for?
Storing multiple values in a single collection
Consolidating like data and modeling real world objects
What are object properties?
Essentially variables attached to an object
Describe object literal notation.
Object name {property name: value…}
How do you remove a property from an object?
Delete operator
What are the two ways to get or update the value of a property?
Dot notation or bracket notation
What are arrays used for?
Storing a list of values with a particular order
Describe array literal notation.
var newArrayName = [];
How are arrays different from “plain” objects?
Arrays are organized into numeric lists
What number represents the first index of an array?
0
What is the length property of an array?
The number of values the array contains
How do you calculate the last index of an array?
The length of the array minus one
What is a function in JavaScript?
A code block that does a particular task and can be reused
Describe the parts of a function definition.
function functionName (parameter(s)names){ code block }
Describe the parts of a function call.
functionName(parater(s)youWantToPass)
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function definition is writing the code for the function
Function call is telling the function and all of it’s to run in that instance
What is the difference between a parameter and an argument?
Parameter is the thing that gets passed within the function definition.
Argument is the thing that gets passed when the function is called.
Why are function parameters useful?
Allow us to give data to functions for functions to perform operations on them
What two effects does a return statement have on the behavior of a function?
Return statement determines the value the function returns
It ends the execution of a function
Why do we log things to the console?
Helps to verify that our code is working as intended and to help debug
For development only
What is a method?
A function on an object
How is a method different from any other function?
A function is a set of instructions that perform a task and a method is a set of instructions that are associated with an object
How do you remove the last element from an array?
Pop()
How do you round a number down to the nearest integer?
Floor method of the math object
Math.floor()
How do you generate a random number?
Random method of the math object
How do you delete an element from an array?
Splice
How do you append an element to an array?
Push method
How do you break a string up into an array?
Use the split method and pass in an empty space (‘ ‘)
Do string methods change the original string? How would you check if you weren’t sure?
No. String are immutable
Console log the strings.
Roughly how many string methods are there according to the MDN Web docs?
Around 50
Is the return value of a function or method useful in every situation?
Not always
Roughly how many array methods are there according to the MDN Web docs?
Around 30-40. (38)
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN (the OFFICIAL source of truth)
Give 6 examples of comparison operators.
> ,
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
Make decisions, compare values, make branching logic
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
If (condition to check{
Code block
}
What are the three logical operators?
! (not), ||(or), &&(and)
How do you compare two different expressions in the same condition?
Using a logical operator
What is the purpose of a loop?
Lets you repeat code over and over
What is the purpose of a condition expression in a loop?
Check to see if the loop should run again
Needs to eventually stop the loop
What does “iteration” mean in the context of loops?
One full pass/run of the code block inside of the loop
When does the condition expression of a while loop get evaluated?
Before each iteration
When does the initialization expression of a for loop get evaluated?
Before each iteration
When does the condition expression of a for loop get evaluated?
At the beginning of each subsequent iteration
When does the final expression of a for loop get evaluated?
The end of each loop iteration and before the next evaluation of condition.
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 the variable
How do you iterate through the keys of an object?
For in loop
Give two examples of document methods that retrieve a single element from the DOM.
QuerySelector and GetElementById
Give one example of a document method that retrieves multiple elements from the DOM at once.
GetElementsByTagName
Why might you want to assign the return value of a DOM query to a variable?
So we don’t have to consciously query the dom
What console method allows you to inspect the properties of a DOM element object?
.dir
What does document.querySelector() take as its argument and what does it return?
Takes an element, id, class (any valid css selector)
Returns the first instance of that selector
What does document.querySelectorAll() take as its argument and what does it return?
Take a css selectors
Returns a NodeList
Why do we log things to the console?
To confirm the data as we write the code
What is the purpose of events and event handling?
Have code run based on when things happen
What method of element objects lets you set up a function to be called when a specific type of event occurs?
Add event listener
What is a callback function?
Any function that we do not call directly
What object is passed into an event listener callback when the event fires?
Event object
It gets named event or e but technically can have any name
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
Point of origin for the event
Check MDN
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
1st one just has a function for the handle click
2nd has a callback function for the handle click
What is the className property of element objects?
Class Attribute value