Javascript Flashcards
What is the purpose of variables?
To store some sort of value
How do you declare a variable?
var varName = value;
How do you initialize (assign a value to) a variable?
varName = newValue;
What characters are allowed in variable names?
Letters, numbers, underscore, $dollar_sign
What does it mean to say that variable names are “case sensitive”?
“THIS” is different from “this” and “tHiS”
What is the purpose of a string?
To represent letters and characters
What is the purpose of a number?
To represent numerical data
What is the purpose of a boolean?
Used as a trigger for a condition
What does the = operator mean in JavaScript?
It’s the assignment operator
How do you update the value of a variable?
Assign a new value on the right side of the = operator
What is the difference between null and undefined?
null points to a nonexistent address
Undefined is a value for variables not yet defined
Why is it a good habit to include “labels” when you log values to the browser console?
It helps you recognize why you are logging onto the console
Give five examples of JavaScript primitives.
Number, string, boolean, array, object
What data type is returned by an arithmetic operation?
number
What is string concatenation?
Joining two or more strings together
What purpose(s) does the + plus operator serve in JavaScript?
String concatenation, addition for numbers
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
Adds the given value and re-assigns the new value to the variable on the left hand side of the operator
What are objects used for?
A data type that groups together a set of variables and functions to model real life ‘objects’
What are object properties?
Variables that hold data unique to that object
Describe object literal notation.
Declared with var objName = { }; Properties are stored between curly braces separated by commas
How do you remove a property from an object?
delete obj.prop;
What are the two ways to get or update the value of a property?
Dot notation and bracket notation with assignment operator
What are arrays used for?
To store related data in an ordered list
Describe array literal notation.
Declare with var arrName
Assign: [val1, val2, …];
How are arrays different from “plain” objects?
You access its elements using indices
What number represents the first index of an array?
0
What is the length property of an array?
It returns the length of an array or how many elements that array contains
How do you calculate the last index of an array?
arr.length - 1
What is a function in JavaScript?
To pack code for reuse throughout a program
Describe the parts of a function definition.
Function keyword, functionName(optional), parameters(optional), {code block}, return value(optional)
Describe the parts of a function call.
Function name and argument(s)
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function call doesn’t have the actual code, the function definition has the ‘function’ keyword
What is the difference between a parameter and an argument?
Parameters are defined in the function definition, arguments are passed in a function call
Why are function parameters useful?
They can be used to store data to be used (and sometimes modified) within a function
What two effects does a return statement have on the behavior of a function?
It exits a function immediately
It pushes a value out to where the function is called
Why do we log things to the console?
To keep track of the changes we make to our code and verifies our data. This prevents hard to find bugs because we’ll see it immediately with the log
What is a method?
Functions that are properties of an object
How is a method different from any other function?
Functions can only be called from an object through dot operator
How do you remove the last element from an array?
arr.pop();
How do you round a number down to the nearest integer?
Math.floor(num)
How do you generate a random number?
Math.random()
How do you delete an element from an array?
arr.splice(startIndex, deleteCount)
How do you append an element to an array?
arr.push(elem);
How do you break a string up into an array?
aString.split(‘delimiter’)
Do string methods change the original string? How would you check if you weren’t sure?
Strings are immutable meaning they cannot be changed. Log to console if you aren’t sure
Roughly how many string methods are there according to the MDN Web docs?
About 35 not including ‘deprecated’ methods (not recommended to use because some browsers won’t support)
Is the return value of a function or method useful in every situation?
No, pop() will return a value, but you don’t need to use it
Roughly how many array methods are there according to the MDN Web docs?
About 40
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?
To evaluate the boolean value of a conditional expression
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
If keyword, conditional statement between parentheses, code block to run if “if” statement is true
What are the three logical operators?
&& and, || or, ! logical not
How do you compare two different expressions in the same condition?
Use a logical operator between the two expressions
What is the purpose of a loop?
To run a block of code multiple times
What is the purpose of a condition expression in a loop?
To check if the loops should advance or not
What does “iteration” mean in the context of loops?
A single pass of the code block
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 expression is checked for the first time
Only gets initialized once
When does the condition expression of a for loop get evaluated?
After initialization and before the block of code runs
Subsequent loops, the condition expression is evaluated after the final (updating) expression
When does the final expression of a for loop get evaluated?
At the end of each 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?
Adds 1 to the variable and reassigns it back into the variable
How do you iterate through the keys of an object?
By using a for..in loop
What event is fired when a user places their cursor in a form control?
focus
What event is fired when a user’s cursor leaves a form control?
blur
What event is fired as a user changes the value of a form control?
input
What event is fired when a user clicks the “submit” button within a ?
submit
What does the event.preventDefault() method do?
prevents the browser from automatically reloading the page with the form’s values in the URL
What does submitting a form without event.preventDefault() do?
Reloads the page
What property of a form element object contains all of the form’s controls.
Elements (?)
What property of a form control object gets and sets its value?
value
What is one risk of writing a lot of code without checking to see if it works so far?
You might miss a hard-to-find bug further down the line
What is an advantage of having your console open when writing a JavaScript program?
To check for bugs or mistakes while writing
What is the event.target?
It references the object in which the event was directed at
What is the effect of setting an element to display: none?
It hides the element from view on the screen
What does the element.matches() method take as an argument and what does it return?
It takes a selector (string) as an argument and it returns boolean value (true if it matches)
How can you retrieve the value of an element’s attribute?
element.getAttribute(‘attributeName’)
Returns the value attached to the attribute
At what steps of the solution would it be helpful to log things to the console?
Pretty much each step logging to the console would be useful
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?
You would have to query each new tab and view DOM element and add an event listener to each one
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
You would have to write a stack of “if” and “else if” statements
What is JSON?
It stands for JavaScript Object Notation
It is a text-based data format which allows transmitting of data across networks
What are serialization and deserialization?
Serialization is converting a native object into a string for transmitting the data
Deserialization is the opposite of that
Why are serialization and deserialization useful?
It allows the transmitting and translating of data that, for the most part, can’t be transmitted
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify(jsonObj)
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse(jsonStr) const jsonStr = ‘{ “prop”:”value” }’; // quotes around prop required
How do you store data in localStorage?
window.localStorage.setItem( ‘prop-name’ , ‘jsonString’ )