Javascript Flashcards
What is the purpose of variables?
Store data/information
How do youdeclarea variable?
Start with variable keyword (const, var, or let) follow by a variable name
How do you initialize (assign a value to) a variable?
Using the assignment operator ‘’=’
What characters are allowed in variable names?
dollarsign ($) and underscore (_), letters and numbers
What does it mean to say that variable names are “case sensitive”?
The cases matter. “one” is not the same as “One”
What is the purpose of a string?
To store text as values
What is the purpose of a number?
To store numeric values
What is the purpose of a boolean?
To show true or false
What does the=operator mean in JavaScript?
assign
• How do you update the value of a variable?
assign a new value to that variable
What is the difference betweennullandundefined?
null is intentionally empty, will be updated later. undefined means a value has been declared by has no value.
Why is it a good habit to include “labels” when you log values to the browser console?
it is much clearer which variables are being logged and in what order. Good for debugging
Give five examples of JavaScript primitives.
string, boolean, number, undefined and null
What data type is returned by an arithmetic operation?
number
What is string concatenation?
Process of joining together two or more strings
What purpose(s) does the+plus operator serve in JavaScript?
it can serve as an addition for arithmetic operators and a string operator for concatenation
What data type is returned by comparing two values (,===, etc)?
boolean
What does the+=”plus-equals” operator do?
adds values and then it re-assigns to same variable
What are objects used for?
grouping together a set of variables and functions with property values and methods; used to model real-life objects
What are object properties?
properties are variables that are part of an object; objects can store any data types
Describe object literal notation.
{}; opening and closing curly braces.
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 and bracket notation
What are arrays used for?
used for storing lists of data, numerically indexed
Describe array literal notation.
square brackets [ ]
How are arrays different from “plain” objects?
numerically indexed from 0 while objects aren’t numerically indexed
What is thelengthproperty of an array?
it measures how long the array is; how many indexes are in an array
How do you calculate the last index of an array?
array[array.length-1]
What number represents the first index of an array?
0
What is a function in JavaScript?
a block of code designed to perform a particular task.
Describe the parts of a function call.
the function name and the arguments being passed inside the parenthesis
When comparing them side-by-side, what are the differences between a function call and a function definition?
The function definition has the parameter and the code block { }. The call takes the arguments that are passed into the function.
What is the difference between a parameter and an argument?
Parameters are variables listed as a part of the function definition. Arguments are values passed to the function when it is invoked
Why are function parameters useful?
allows you to pass information into the function
What two effects does a return statement have on the behavior of a function?
returns the value of code and stops executing the code block
Why do we log things to the console?
To debug and check data
What is a method?
A method is a function which is a property of an object.
How is a method different from any other function?
Methods exist as a property on an object
How is a method different from any other function?
Methods exist as a property on an object/ methods can use items in the object.
How do you round a number down to the nearest integer?
Math.floor( ) method
How do you generate a random number?
Math.random( ) method
How do you delete an element from an array?
.splice ( ), .pop ( ), .shift( )
How do you delete an element from an array?
.splice ( ) method
How do you append an element to an array?
.push ( ) method
How do you break a string up into an array?
.split( ) method
Do string methods change the original string? How would you check if you weren’t sure?
no; check by using the console log & MDN
Roughly how many string methods are there according to the MDN Web docs?
A lot; 40ish
Roughly how many array methods are there according to the MDN Web docs?
A lot; more than 30
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.
Less than () Greater than or equal (>=) Loosely equal to (==) Strictly equal to (===) Not loosely equal to (!=) Not strictly equal to (!==)
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
A conditional statement to check whether or not to do certain actions/ make a decision
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
a “if”keyword followed by a condition in parentheses ( ) and an opening curly brace for the code block and closing curly brace
What are the three logical operators?
&& (and / ampersand),
| | (or/ double pipe),
! (not / bang)
How do you compare two different expressions in the same condition?
logical operators
What is the purpose of a loop?
to repeat an action/code
What is the purpose of a condition expression in a loop?
the purpose to sets the rules for the loop and tell it when to end.
What does “iteration” mean in the context of loops?
the number of times it runs
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 the condition/the loops start
When does the condition expression of a for loop get evaluated?
1
When does the final expression of a for loop get evaluated?
end of each loop iteration
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?
increase the value by 1
How do you iterate through the keys of an object?
for in loop
Why do we log things to the console?
to test and to debug code
What is a “model”?
a recreation of something to be used as an example
Which “document” is being referred to in the phrase Document Object Model?
the HTML document
What is the word “object” referring to in the phrase Document Object Model?
the elements
What is a DOM Tree?
a recreation of the html elements on the page that can be acc
Give two examples of document methods that retrieve a single element from the DOM.
getElementById(), querySelector()
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementByClassName()
querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
to store the location of the element and to stop searcgubg
What console method allows you to inspect the properties of a DOM element object?
the dir method of the console object
Why would a tag need to be placed at the bottom of the HTML content instead of at the top
JS needs to load after the HTML - so the DOM can load first
What does document.querySelector() take as its argument and what does it return?
an element selector and that selector
What does document.querySelectorAll() take as its argument and what does it return?
CSS selector and returns the NodeList
Why do we log things to the console?
checking functionality and check data; test code and debug
What is the purpose of events and event handling?
Events - the browser’s way of indicating when something has happened: page finished loading, button has been clicked
Event handlers- let you indicate which event you are waiting for on any particular element.
Overall - to trigger a function; make the page feel more interactive
What method of element objects lets you set up a function to be called when a specific type of event occurs?
. addEventListener( ) method
What is a callback function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
What object is passed into an event listener callback when the event fires?
the event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
It is a reference to the value of the object onto which the event was dispatched. Check with MDN
What is the difference between these two snippets of code?
- element.addEventListener(‘click’, handleClick)
- element.addEventListener(‘click’, handleClick())
The first one is a callback function and second one is calling the function. The second one immediately calls the function
What is the difference between these two snippets of code?
- element.addEventListener(‘click’, handleClick)
- element.addEventListener(‘click’, handleClick())
The first one is a callback function and second one is calling the function. The second one immediately calls the function
What is the className property of element objects?
The className property of the Element interface gets and sets the value of the class attribute of the specified element.