JavaScript Flashcards
What is the purpose of a loop?
repeat code until a condition criteria is met
What is the purpose of a condition expression in a loop?
determine an end state of the loop
What does iteration mean in the context of a loop?
pass through the loop code
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
beginning, only run once
When does the condition expression of a for loop get evaluated?
before every iteration
When does the final expression of a for loop get evaluated?
after the loop code block
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
How do you iterate through the keys of an object?
for in loop
Why do we log things to the console?
To check for bugs, double check our work.
What is a “model”?
A copy or representation of the original
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.
querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
So the browser does not need to search for it every time it needs it
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?
The browser needs to load all the HTML before JS can be run on it.
What does document.querySelector() take as its argument and what does it return?
an element, id, or class returns the first element it finds with matching element, id, or class
What does document.querySelectorAll() take as its argument and what does it return?
takes element, id, or class
returns all elements with that tag, id, or class
Which document is being referred to in the phrase DOM
HTML
What is the word object referring to in DOM
refers to JS objects
What is a DOM tree
model of the html document represented as a JS object (DOM generally), tree is series of objects elaborating on a chunk of the html document.
What is the purpose of events and event handling?
add dynamic content, user interactivity, respond to an occurrence
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?
function definition passed as a value so it may be invoked later.
What object is passed into an event listener callback when the event fires?
the event object with a target object. All possible data pertinent to the event.
What is the event.target? If you werent sure, how would you check? Where could you get more information about this?
The place where the event occurred. First element most immediate to where the event occurred.
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
you called one function, the other will be called later by the browser.
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 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 an elements default action
What property of a form element object contains all of the form’s controls
elements property
What property of form a 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?
Don’t know where your code is broken if it is
What is an advantage of having your console open when writing a JavaScript program?
You immediately know where your error is
What is the event.target
The element the event originated from
Why is it possible to listen for event on one element that actually happen its descendent elements?
Bubbling (child to parents)
What DOM element property tells you what type of element it is
tagName
What does the element.closest() method take as its argument and what does it return?
css selector and return something the matches the selector (starts nested, queries out)
How can you remove an element from the DOM
Call remove on the element itself
At what steps of the solution would it be helpful to log things to the console?
Every step
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JS code be written instead.
Add event listener to every tab
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JS code be written instead?
lots of conditionals/conditions
What is JSON?
data exchange format
What are serialization and deserialization?
object to string, string to object
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse
How do you store data in localStorage?
setItem(‘key’, value)