JavaScript Flashcards
What is the purpose of variables?
Purpose of variables is to store data/information.
How do you declare a variable?
Declaring a variable you need the variable keyword: var and giving it a name: variable name.
How do you initialize (assign a value to) a variable?
You assign a value by using the assignment operator: ‘=‘
What characters are allowed in variable names?
Characters allowed in the variable names are letters, dollar sign, and underscore
What does it mean to say that variable names are “case sensitive”?
When variables are “case sensitive” the same named variable is different when one of the words are capitalized.
What is the purpose of a string?
Purpose of a string is to add new content into a page.
What is the purpose of a number?
Purpose of a number is calculating, determining the size of the screen, moving the position of an element, or setting the amount of time an element should take to fade in.
What is the purpose of a boolean?
Purpose of a boolean is to determine which part of a script should run.
What does the = operator mean in JavaScript?
“=“ means the value is being assigned to a variable
How do you update the value of a variable?
You use the same variable and assign it to the new value
What is the difference between null and undefined?
undefined is a variable that hasn’t been defined to anything and null is a variable that is intentionally missing a value.
Why is it a good habit to include “labels” when you log values to the browser console?
Labels can help us with what we’re referring to specific variables/values.
Give five examples of JavaScript primitives.
5 primitives: null, undefined, string, boolean, number
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
When you’re joining multiple strings into 1 string
What purpose(s) does the + plus operator serve in JavaScript?
Adding values and concatenation strings
What data type is returned by comparing two values (<, >, ===, etc)?
Boolean
What does the += “plus-equals” operator do?
It adds the value to a variable and then assigns the result to the variable
What are objects used for?
Objects are used to store/group data
What are object properties?
Object properties are variables associated with the object
Describe object literal notation.
Object Literal Notation consists of the variable object with properties and methods, and in the properties consists of keys and values, methods consists of functions.
How do you remove a property from an object?
delete
What are the two ways to get or update the value of a property?
dot or bracket notations
What is a function in JavaScript?
Its a set a statements that perform a task
Describe the parts of a function definition.
function keyword, name of the function, parameter, and curly braces
Describe the parts of a function call.
function name and parenthesis
When comparing them side-by-side, what are the differences between a function call and a function definition?
definition uses parameters and has a function code block
call uses arguments to pass in
What is the difference between a parameter and an argument?
Parameters stores variables that can be used later and is used in a function definition
argument is used in a function call
Why are function parameters useful?
You pass in different arguments in the parameters
What two effects does a return statement have on the behavior of a function?
It stops the code from running
it will returns any value in the code block
Why do we log things to the console?
It tells us what the code is doing
What is a method?
Method is a property of an object that has a function definition
How is a method different from any other function?
Methods are assigned with an object property, while a function is not
How do you remove the last element from an array?
pop() method
How do you round a number down to the nearest integer?
floor() method
How do you generate a random number?
random() method
How do you delete an element from an array?
shift() method, pop() method, or splice() method
How do you append an element to an array?
push() method, or unshift() 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?
Does not change.
console.log() to check
Roughly how many string methods are there according to the MDN Web docs?
Roughly 50 string methods
Is the return value of a function or method useful in every situation?
No
Roughly how many array methods are there according to the MDN Web docs?
Roughly 40 array methods
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.
6 comparison operator: equal (=), not equal (!=), strictly equal (===), strictly not equal (!===), greater than (>), greater than or equal (>=), less than (<), less than or equal (<=).
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
It is to run the code if the condition is met
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if (condition) {
statement
} else {
statement
}
What are the three logical operators?
logical AND (&&), logical OR (| |), logical NOT (!)
How do you compare two different expressions in the same condition?
Compare two expressions with the logical operator
What is the purpose of a loop?
Its to repeat a task a set number of times.
What is the purpose of a condition expression in a loop?
To cause a loop to end.
What does “iteration” mean in the context of loops?
It is when the condition passes in the loop.
When does the condition expression of a while loop get evaluated?
Before executing the statement
When does the initialization expression of a for loop get evaluated?
The beginning of the loop
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 interation
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
When the condition expression evaluates to false
What does the ++ increment operator do?
It adds one to variable in the initial conditional expression
How do you iterate through the keys of an object?
for … in loop
Why do we log things to the console?
It tells us what the code is doing
What is a “model”?
The model is the DOM tree made up of objects. Any representation of the DOM.
Which “document” is being referred to in the phrase Document Object Model?
The document is the webpage; it can be displayed in the browser or the HTML source.
What is the word “object” referring to in the phrase Document Object Model?
“object” refers to the object of the document.
What is a DOM Tree?
DOM Tree is a model of a web page.
Give two examples of document methods that retrieve a single element from the DOM.
To get individual nodes, you use getElementById() or querySelector.
Give one example of a document method that retrieves multiple elements from the DOM at once.
To get multiple elements at once, you use getElementByClassName() - returns an HTML collection, getElementsByTagName(), or querySelectorAll() - returns a static NodeList.
Why might you want to assign the return value of a DOM query to a variable?
Because you might work with an element more than once.
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?
<script> is placed at the end so it gives time for the HTML load before any javascript code runs. This will speed up the website response time. </script>
What does document.querySelector() take as its argument and what does it return?
querySelector() uses a CSS selector as the argument and returns the first matching element.
What does document.querySelectorAll() take as its argument and what does it return?
querySelectorAll() uses a CSS selector as the argument and returns all matching elements.
Why do we log things to the console?
To see what the code is doing
What is the purpose of events and event handling?
Purpose of events is to make the page feel interactive for the user.
Purpose of event handling is to run a code when the event takes place that allows web pages to respond.
Are all possible parameters required to use a JavaScript method or function?
No, you don’t have to use parameters
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener() method lets you set up a function to be called when a specific type of event occurs.
What is a callback function?
callback function is a function passed into another function as an argument.
What object is passed into an event listener callback when the event fires?
event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
event.target is represents with the element that is interacted with.
check MDN
What is the className property of element objects?
Value of the class attribute
How do you update the CSS class attribute of an element using JavaScript?
className property
What is the textContent property of element objects?
Text content of the node
How do you update the text within an element using JavaScript?
textContent property
Is the event parameter of an event listener callback always useful?
No
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
More complicated
Why is storing information about a program in variables better than only storing it in the DOM?
It will be more accurate because the DOM can be manipulated therefore the information may be changed.
What event is fired when a user places their cursor in a form control?
focus event
What event is fired when a user’s cursor leaves a form control?
blur event
What event is fired as a user changes the value of a form control?
input event
What event is fired when a user clicks the “submit” button within a <form>?
submit event
What does the event.preventDefault() method do?
Prevents the browser to automatically reload the page
What does submitting a form without event.preventDefault() do?
What property of a form element object contains all of the form’s controls.
elements property
What property of a form control object gets and sets its value?
value property
What is one risk of writing a lot of code without checking to see if it works so far?
You won’t know what part of the code in correct, unless you use the debugger that can be time consuming
What is an advantage of having your console open when writing a JavaScript program?
You know when you make a mistake in your code
Does the document.createElement() method insert a new element into the page?
no