JavaScript Flashcards
What is the purpose of variables?
To store data for use
How do you initialize (assign a value to) a variable?
variable name = (assignment operator) variable value;
What characters are allowed in variable names?
letters, dollar sign, underscore, numbers (cannot start with a number)
What does it mean to say that variable names are “case sensitive”?
variable with that same name but different casing is a different variable
What is the purpose of a string?
store and use text data
What is the purpose of a number?
store and use numeric data
What is the purpose of a boolean?
boolean is a data type with two values true/false can be used to for decision making
What does the = operator mean in JavaScript?
assignment operator, assigns value to a variable
How do you update the value of a variable?
variable name = (assignment operator) new value
What is the difference between null and undefined?
o undefined: computationally empty value and type are undefined o null: means nothing value is null type is object
Why is it a good habit to include “labels” when you log values to the browser console?
to see what is being logged and in what order
Give five examples of JavaScript primitives.
o string o number o boolean o undefined o null
What data type is returned by an arithmetic operation?
number
What is string concatenation?
Join two or more strings together
What purpose(s) does the + plus operator serve in JavaScript?
o Addition
o Concatenation
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds the value of the variable to the right operand and assigns the result to the variable i.e.:
x += y means: x = x + y
What are objects used for?
grouping related data variables (properties) and functionality functions(methods) together
What are object properties?
key: value pairs
data attached to a name, data may change, name will stay the same
a variable attached to a object
Describe object literal notation.
const/let objName = { } can include properties and methods
How do you remove a property from an object?
delete (keyword) object.(member operator)property
What are the two ways to get or update the value of a property?
o Dot notation object.(member operator)property/method = (assignment operator) value
o Bracket notation object[“property”] = (assignment operator) value
What are arrays used for?
storing a list of values
Describe array literal notation.
var keyword arrayName = (assignment operator) [value, value, value];
How are arrays different from “plain” objects?
arrays are ordered and use numbered indices instead of property names
What number represents the first index of an array?
0
What is the length property of an array?
holds the 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 that performs a task
Describe the parts of a function definition.
o Function keyword
o Function name (optional)
o Opening and closing parenthesis which may include a comma separated list of parameters
o A code block surrounded by curly braces
o Return statement (optional)
Describe the parts of a function call.
o Function name
o Opening and closing parenthesis which may include a comma separated list of arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function call does not have the function keyword or the code block surrounded by curly braces and the parameters are now arguments
What is the difference between a parameter and an argument?
o A parameter is declared when defining a function to hold the place for a value
o Arguments are the values that are passed to the function when it is called
Why are function parameters useful?
o Provide data to the function
o Can provide different data for different results without having to repeat code
What two effects does a return statement have on the behavior of a function?
o Causes the function to produce a value for use
o Exits the function and prevents any more of the function’s code block from being run
Why do we log things to the console?
For debugging and to check output
What is a method?
A function that is a property of an object
How is a method different from any other function?
Attached to object so able to access the information on that object so has greater functionality.
How do you remove the last element from an array?
.pop(); method
How do you round a number down to the nearest integer?
Math.floor(number);
How do you generate a random number?
Math.random();
0 to .9repeating
How do you delete an element from an array?
.splice(index, number); method
How do you append an element to an array?
.push(); method
How do you break a string up into an array?
.split(pattern ie ‘ ‘); method
Do string methods change the original string?
No, strings are immutable the only way to save a string with changes is to create a new variable to hold new string
Is the return value of a function or method useful in every situation?
Not if you don’t need to see or use the return value at the moment.
Give 6 examples of comparison operators.
o === strictly equal to (value and type) o !== not strictly equal to (value and type) o > greater than o > less than o >= greater than or equal to o <= less than or equal to
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
To make a decision. Evaluates a condition and if true executes the code block.
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
o if keyword
o condition surrounded by parentheses
o code block surrounded by curly braces
What are the three logical operators?
o && logical and
o || logical or
o ! logical not
How do you compare two different expressions in the same condition?
(expression1 > comparison operator expression2)
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?
Defines the condition required to run the code block, once false loop ends
What does “iteration” mean in the context of loops?
Loop repetition
When does the condition expression of a while loop get evaluated?
Before each loop iteration
When does the initialization expression of a for loop get evaluated?
One time before the first time the condition is evaluated.
When does the condition expression of a for loop get evaluated?
Before each loop iteration.
When does the final expression of a for loop get evaluated?
At the end of each loop iteration 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 its operand and returns a value
How do you iterate through the keys of an object?
for in loop
Why do we log things to the console?
To check output and for debugging
What is a “model”?
A representation/recreation of something
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 JavaScript objects that are modelling the HTML document
What is a DOM Tree?
A model of an element and all of it’s children
Give two examples of document methods that retrieve a single element from the DOM.
o getElementById() uses the value of an element’s id attribute o querySelector() uses a CSS selector, and returns the first matching element
Give one example of a document method that retrieves multiple elements from the DOM at once.
o querySelectorAll() uses a CSS selector to select all matching elements o getElementsByClassName() selects all elements that have a specific value for their class attribute o getElementsByTagName() selects all elements that have the specified tag name
Why might you want to assign the return value of a DOM query to a variable?
To store the reference for it for future use, don’t have to search the whole DOM tree again.
What console method allows you to inspect the properties of a DOM element object?
.dir()
Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?
The browser needs to analyze all of the elements in the HTML page first.
What does document.querySelector() take as its argument and what does it return?
A string holding a CSS selector, the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
A string holding a CSS selector, a node list of all matching elements
What is the purpose of events and event handling?
o To respond to the actions of our users.
o Events are what happens
o Event listeners to trigger functions when an event occurs
What do [] square brackets mean in function and method syntax documentation?
optional
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 passed into another function as an argument
What object is passed into an event listener callback when the event fires?
The event object which contains all the information about that event.
What is the event.target?
The target property of the event object the value of which is the element that the event originated from
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
- the function is passed as an argument (callback function)
2. the function is called and will be replaced by the return of the function call
What is the className property of element objects?
get and set the value of a class attribute
How do you update the CSS class attribute of an element using JavaScript?
element.className = ‘newValue’
What is the textContent property of element objects?
get and set the value of text content of an element and it’s descendants
How do you update the text within an element using JavaScript?
element.textContent = ‘new value’
Is the event parameter of an event listener callback always useful?
No, don’t always need the info stored in event.
Why is storing information about a program in variables better than only storing it in the DOM?
Easy to see and use values multiple times, faster than searching the DOM tree every time
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?
submit
What does the event.preventDefault() method do?
prevents the default behavior of the event
What does submitting a form without event.preventDefault() do?
refreshes the page and adds the form’s values to the URL
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