Javascript Flashcards
Difference between var and let
var is not scope attached, let is scope attached
Does Javascript supports autoconversion?
Yes
“This” in a method
Refers to the owner scope, or to the global scope
Style from HTML
document.getElementById(“id”).style.fontSize
Class from HTML
document.getElementById(“id”).className
“This” alone
Refers to the global scope
“This” in an event
Refers to the element that receives the event
setTimeout()
Receives function and delay in miliseconds. Call function after delay.
setInterval()
Receives function and delay in ms. Repeat function in the said delay time.
clearInterval()
Receives the id from the interval. Stops the interval.
What does sort() by default?
Sorts an array alphabetical and ascending.
How to sort numeric arrays?
(a, b) => a-b , this will sort ascending.
unshift()
Adds new elements to the beginning of an array. Overwites the original array. Returns the new lenght of the array.
push()
Adds new items to the end of an array.Overwites the original array. Returns the new lenght of the array.
shift()
Removes the first item of an array. Changes the original array. Returns the shifted element.
pop()
Removes the last element of an array.Changes the original array. Returns the popped element.
delete array[0]
Delete values in the array leaving undefined values.
concat()
Creates a new array by merging (concatenating) existing arrays. It does not change the existing arrays. It always returns a new array.
flat()
Reduces the dimensionality of an array. Returns a new array.
splice()
Receives position, elements to delete, and values to add. Add and remove elements from array. Chnage the original array, return an array from the deleted values.
slice()
Receives start and end. Return a shallow copy of the array. Does not affect the orginal array.
Shallow copy
Copy an object without appointing to the same memory at first level. But nested objects gonna be appointing to the same memory space.
Object.assign()
It takes at least two objects and fusion them creating a shallow copy.
Spread operator
{ …object, otherproperty: “” } creates a new shallow copy object fusioning the new properties with the introduced objects.
Deep copy
creates an indepent space memory clone of the object
How do you deep copy?
You can use JSON.parse to an stringify object, but functions and objects as properties will be affected. install lodash and use _.copyDeep() to a whole deep copy.
reduce()
It is an iterator that receives a callback that sets accumulator and current value. It returns the final value of the accumulator.