JavaScript Flashcards
How do you deserialize a JSON string into a data structure using Java
Script?
To Deserialize a JSON into a JavaScript object, here we will use a common method JSON.parse() method.
How do you serialize a data structure into a JSON string using JavaScript?
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
What are serialization and deserialization?
Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.
What is JSON?
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax.
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?
We would have to add an if statement for every tab loop well loops through everything
String being assigned to the classname property of the tabs object at tabsindex
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?
Need to do query selector for each individual tab rather than query selector all
How can you retrieve the value of an element’s attribute?
To get the value of an attribute on a specified element, you call the getAttribute() method of the element:
let value = element.getAttribute(name);
What does the element.matches() method take as an argument and what does it return?
The matches() method checks to see if the Element would be selected by the provided selectorString – in other words – checks if the element “is” the selector.
What is the affect of setting an element to display: none?
display: none; When you set the value of display to none, the affected element will disappear
If 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?
addEventlistener on the parent element
Clicktarget put in a common element and just listen for clicks on
How can you remove an element from the DOM?
The Element.remove() method removes the element from the tree it belongs to.
What does the element.closest() method take as its argument and what does it return?
The closest() method traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null
What DOM element property tells you what type of element it is?
Event.target same thing as querySelector elements have a tagName property
Why is it possible to listen for events on one element that actually happen its descendent elements?
Due to event flow the event bubbles
What is the event.target?
event.target returns the DOM element that triggered an specific event, so we can retrieve any property/ attribute that has a value.
When does the ‘beforeunload’ event fire on the window object?
its resources are about to be unloaded
What data type can localStorage save in the browser?
string
How to you retrieve data from localStorage?
getItem()
How do you store data localStorage
setItem()
What is the purpose of variables?
Variables are containers for storing data (storing data values).
How do you initialize (assign a value to) a variable?
In JavaScript, the equal sign (=) is an “assignment” operator
What is the purpose of a string?
JavaScript strings are for storing and manipulating text. A JavaScript string is zero or more characters written inside quotes.
What is the purpose of a number?
Number is a primitive wrapper object used to represent and manipulate numbers like 37 or -9.25 . The Number constructor contains constants and methods for working with numbers.
What is the purpose of a boolean?
Boolean is a datatype that returns either of two values i.e. true or false. In JavaScript, Boolean is used as a function to get the value of a variable, object, conditions, expressions, etc. in terms of true or false
What does the = operator mean in JavaScript?
Assignment
What is the difference between null and undefined?
In JavaScript, undefined means a variable has been declared but has not yet been assigned a value
Why is it a good habit to include “labels” when you log values to the browser
Organization
clarity
Give five examples of JavaScript primitives.
undefined, null, boolean, string and number
What data type is returned by an arithmetic operation?
single numerical value
What is string concatenation?
The JavaScript concatenation operator is a plus sign (+). This operator lets you add the contents of two or more strings together to create one
What purpose(s) does the + plus operator serve in JavaScript?
The + operator is used to perform both addition and string concatenation in JavaScript
What data type is returned by comparing two values (, ===, etc)?
Comparison operators — operators that compare values and return true or false
What does the += “plus-equals” operator do?
The plus-equals operator ( += ) adds the value on the right, to the variable on the left, and then assigns that value back into the variable on the left
What are object properties?
property is an association between a name (or key) and a value.
Describe object literal notation.
Object property value
How do you remove a property from an object?
delete.property
How do you calculate the last index of an array?
Object.length-1 students[students.length - 1]
What is the length property of an array?
Stores a count of how many items are in an array
How do you calculate the last index of an array?
Object.length-1 students[students.length - 1]
Why do we log things to the console?
The console.log() method outputs a message to the web console
What is a method?
A method is a function which is a property of an object. There are two kind of methods: Instance Methods which are built-in tasks performed by an object instance, or Static Methods which are tasks that are called directly on an object constructor.
How do you remove the last element from an array?How do you round a number down to the nearest integer?How do you round a number down to the nearest integer?
pop() method.
How do you generate a random number?
Math.random()
How do you delete an element from an array?
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place
How do you append an element to an array?
1) The push() method adds one or more elements to the end of an array and returns the new length of the array. …
2) The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array: var a = [1, 2, 3]; a.
How do you break a string up into an array?
The split() method
Do string methods change the original string? How would you check if you
weren’t sure?
No they don’t
Roughly how many string methods are there according to the MDN Web docs?
38ish
Is the return value of a function or method useful in every situation?
No ,ex push gives return value that is not needed length may not be needed at the moment
Roughly how many array methods are there according to the MDN Web docs?
50ish
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.
,>=,<=, ===,!==
What data type do comparison expressions evaluate to?
single Boolean value of either True or False
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
check the expression to see whether a condition is met and returns a value based on the output obtained
What are the three logical operators?
||,&&, !
How do you compare two different expressions in the same condition?
Logical oeprators
What is the purpose of a loop?
to repeat the same, or similar, code a number of times
What is the purpose of a condition expression in a loop?
the stopping/breaks of an expression of the loop
What does “iteration” mean in the context of loops?
number of times loop will be repeated
What does “iteration” mean in the context of loops?
number of times loop will be repeated
When does the condition expression of a while loop get evaluated?
each loop iteration
When does the initialization expression of a for loop get evaluated?
as the loop begins
When does the final expression of a for loop get evaluated?
each time after the code block runs
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 and reassigns value
How do you iterate through the keys of an object?
for in loop
How do you serialize a data structure into a JSON string using JavaScript?
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.