JS Flashcards
How to you store data in localStorage?
localStorage.setItem(‘myCat’, ‘Tom’);
How to you retrieve data from localStorage?
localStorage.getItem(‘myCat’);
What data type can localStorage save in the browser?
only strings
When does the ‘beforeunload’ event fire on the window object?
when the document and it’s resources are about to be unloaded, or closing the browser
What is JSON?
text-based data format following JavaScript object syntax
What are serialization and deserialization?
Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network. Deserialization is the reverse process: turning a stream of bytes into an object in memory.
How do you serialize a data structure into a JSON string using JavaScript?
JSON.Strignify
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.Parse
What is the event.target?
target property of the Event interface is a reference to the object onto which the event was dispatched.
What is the affect of setting an element to display: none?
it makes it invisible and takes it out of document
What does the element.matches() method take as an argument and what does it return?
it takes an argument of css selector, returns a boolean, weather css selector matches the element
How can you retrieve the value of an element’s attribute?
get attribute method
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 JavaScript code be written instead?
you would have to list every tab element with multiple listeners
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 need a list of explicit conditionals,
What is the event.target?
target property of the event interface, reference to the object which event was dispatched
Why is it possible to listen for events on one element that actually happen its descendent elements?
because of the default document flow, which is set to event bulling, so starts at the most specific elements, then less specific
What DOM element property tells you what type of element it is?
event.target
What does the element.closest() method take as its argument and what does it return?
the argument is a selector and it returns it’s self or nearest ancestor element
How can you remove an element from the DOM?
use .remove method
f you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?
you would add an event listener to the parent, using event delegation
Does the document.createElement() method insert a new element into the page?
no, it just simply create the element,that’s going to be added to the dom
How do you add an element as a child to another element?
append child method
What do you pass as the arguments to the element.setAttribute() method?
attribute name and the value
What steps do you need to take in order to insert a new element into the page?
create element, grab the spot you want to append it to, append it
What is the textContent property of an element object for?
assigning text content to the new element, and also for knowing the text content in the text element
Name two ways to set the class attribute of a DOM element.
assigning a new name to the class property and the other way is set attribute
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
repetition and make it dynamic
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?
if the event is not explicitly handled, its default action should be taken
What does submitting a form without event.preventDefault() do?
refreshes the page and adds the data on the url
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 property