JavaScript Flashcards
What is the purpose of variables?
To store data that needs to be referenced later
How do you declare a variable?
Using the keyword var (const and let)
How do you initialize (assign a value to) a variable?
var variableName = value;
What characters are allowed in variable names?
Letters, numbers, $, _ (numbers cannot be first)
What does it mean to say that variable names are “case sensitive”?
They are sensitive to uppercase and lowercase letters
What is the purpose of a string?
Store a series of character, text content
What is the purpose of a number?
To store a numeric value, counting, measurement, math
What is the purpose of a boolean?
A data type that only has two possible values, to make decisions
What does the = operator mean in JavaScript?
Assigning a value to something
How do you update the value of a variable?
variableName = newValue
What is the difference between null and undefined?
Null: empty value, must be assigned by you, it represents something that does not have a value assigned to it yet,
Undefined: empty value, assigned by JavaScript
Where is var scoped
Var is scoped to the function that it is inside of. If you leave out the var keyword when using a variable in a function, JavaScript will look for the next variable that is scoped closest
Why is it a good habit to include “labels” when you log values to the browser console?
So you know what is being logged to the console
Give five examples of JavaScript primitives.
String, number, boolean, undefined, null
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
Combining a string with another value and makes the result of that expression into a new string
What purpose(s) does the + plus operator serve in JavaScript?
Adding numbers, concatenating strings
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
Adds the value of the variable on the right to the variable on left, then assigns the result of that expression to the variable on the left
What are objects used for?
Storing data in a group or collection that makes sense
What are object properties?
Pieces of data stored within an object
Describe object literal notation.
Var variableName = {};
How do you remove a property from an object?
The delete operator: delete object.property
What are the two ways to get or update the value of a property?
Dot notation: object.propertyName
Bracket notation: object[‘propertyName’]
What are arrays used for?
To list data in a specific order
Describe array literal notation.
Var variableName = [];
How are arrays different from “plain” objects?
They are numerically indexed
What number represents the first index of an array?
0
What is the length property of an array?
How many elements are in the array
How do you calculate the last index of an array?
Array.length - 1
What is a function in JavaScript?
A special type of object that can be called, a set of reusable code
Describe the parts of a function definition.
Function functionName(parameter) {code block; return statement} Name and return statement are optional
Describe the parts of a function call.
functionName(argument) argument is optional
When comparing them side-by-side, what are the differences between a function call and a function definition?
A call will have arguments, a definition includes a code block
What is the difference between a parameter and an argument?
A parameter is a variable related to a function definition, an argument is a value related to a function call
Parameter is a placeholder for a variable that will eventually be passed into the function. It is included in the function definition
Argument is the actual variable that is passed into the function when it is called
Why are function parameters useful?
To pass additional information into a function for the function to use. It allows us to create more generalized, reusable functions
What two effects does a return statement have on the behavior of a function?
Causes the function to produce a value, ends the function
Why do we log things to the console?
To test and inspect your code and make sure your functions or methods are returning what you expect, or see what the values of variables are.
What is a method?
A property of an object that is a function
How is a method different from any other function?
A method is a function that is a property of an object
How do you remove the last element from an array?
array.pop()
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
Math.random() (generates a number from 0 to 1 not including 1)
How do you delete an element from an array?
Array.splice(start, deleteCount)
How do you append an element to an array?
Array.push()
How do you prepend an element to an array?
Array.unshift()
How do you break a string up into an array?
String.split()
Do string methods change the original string? How would you check if you weren’t sure?
No, strings are immutable. Use console.log() to check
Is the return value of a function or method useful in every situation?
no
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN, also include the name of the language
Give 6 examples of comparison operators.
> , =, <=, !=, !==
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
To execute a block of code if a certain condition is truthy
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
If (condition) {code block}
What are the three logical operators?
&&, || , !
How do you compare two different expressions in the same condition?
Put each expression in parentheses
What is the purpose of a loop?
To allow us to repeat a certain block of code
What is the purpose of a condition expression in a loop?
To tell the loop whether or not to run
What does “iteration” mean in the context of loops?
It represents each time the code within the code block 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?
Only once, before the loop starts
Initialization: before anything
When does the condition expression of a for loop get evaluated?
After initialization, before each iteration
Condition: before each
When does the final expression of a for loop get evaluated?
After each iteration
Final: after each
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?
Increments a variable by 1
How do you iterate through the keys of an object?
a for…in loop
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?
JavaScript object
What is a DOM Tree?
A tree representing each element within the DOM, and its child elements
Give two examples of document methods that retrieve a single element from the DOM.
document.getElementById(), document.querySelector()
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
So you can access it again more easily
What console method allows you to inspect the properties of a DOM element object?
console.dir()
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
So the browser can parse through the document before JavaScript has to access it
What does document.querySelector() take as its argument and what does it return?
A string of a CSS selector, returns the first element that matches the selector
What does document.querySelectorAll() take as its argument and what does it return?
A string of a CSS selector, returns a NodeList of all elements that match
What is the purpose of events and event handling?
It allows us to interact with the webpage, to make our apps dynamic
Are all possible parameters required to use a JavaScript method or function?
no
What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener()
What is a callback function?
A function that is passed into another function
What object is passed into an event listener callback when the event fires?
The event object, which is a report on the event
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The element where the event we are dealing with originated from. console.log() it. MDN
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
We are actually calling the function in the second one, which means that the function will run at that line of code, not when the event actually fires
What is the className property of element objects?
Used to access or update the value of the class attribute on an element
How do you update the CSS class attribute of an element using JavaScript?
Assign a new value to the className property of that element
What is the textContent property of element objects?
Used to access or update text content of any DOM element
How do you update the text within an element using JavaScript?
Assign a new value to the textContent property of the element
Is the event parameter of an event listener callback always useful?
no
Why is storing information about a program in variables better than only storing it in the DOM?
We don’t want to check outside of your JavaScript every time you need to access data
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 “form” element?
submit
What does the event.preventDefault() method do?
Prevents the default behavior of an event from happening
What does submitting a form without event.preventDefault() do?
Refresh the page
What property of a form element object contains all of the form’s controls.
.elements
What property of form a control object gets and sets its value?
.value