Vanilla Flashcards
(30 cards)
Difference between const, let, var and <blank>?</blank>
How to create a shallow copy from an iterable object?
Array.from()
- let newArray = Array.from(someArray);*
- let newArray = Array.from(“string is iterable in JS”);*
- —-*—————————————————-
How to check if something is an Array?
Array.isArray()
Array.isArray([]); //true
Array.isArray([1]); //true
Array.isArray(new Array()); //true
Array.isArray({}); //false
Array.isArray(null); //false
Array.isArray(undefined); //false
Array.isArray(100); //false
Array.isArray(‘Array’); //false
How to concat Arrays?
Array.prototype.concat(Array.prototype)
note, this is a shallow copy
- var arr = [1,2];*
- var arr2 = [[1,2], [3,4]];*
- var newArr = arr.concat(arr2) // [1,2,[1,2],[3,4]]*
- The new Array is the shallow copy of the arguments so if we change the source it affects the copied object.*
- arr2[0][1] = 9; //[1,2,[1,9],[3,4]]*
Difference between SomeType.someMethod() and SomeType.prototype.someMethod()?
no prototype means you call that method as a class method and pass it an object of that type.
prototype means that method can be called as an instance method from that object instance.
Difference between for..in and for..of
for. .in gives the keys of an iterable (keys for an Object, index for an Array
for. .of gives the values of an iterable (nothing for an Object, elements for an Array)
How to add element to beginning of Array?
How to remove element from beginning of Array?
How to add element to end of Array?
How to remove element from end of Array?
Array.prototype.unshift()
Array.prototype.shift()
Array.prototype.push()
Array.prototype.pop()
How to concatenate all array elements?
Array.prototype.join([separator])
How find index of search query in array?
arr.indexOf(searchElement[, fromIndex])
will return only the first it finds with this, need to write a loop to grab all
How to filter array with a given condition
var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
How to see if an Array has a value?
arr.includes(valueToFind[, fromIndex])
How to see if an Array has any values that pass a certain condition?
arr.some(callback(element[, index[, array]])[, thisArg])
How to see if an Array has any values that pass a certain condition?
arr.every(callback(element[, index[, array]])[, thisArg])
How to return a new Array with each element modified by a function
var new_array = arr.map(function callback(currentValue[, index[, array]]) { // Return element for new_array }[, thisArg])
How to sort an array?
arr.sort([compareFunction])
How to modify an Array in place by adding/remove/modyfing elements with given argument?
var arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, …]]]])
How to select a portion of an array with a given start and possibly end index?
arr.slice([begin[, end]])
How to create objects with constant params or values?
Object.seal(someObject) and Object.freeze(someObject)
seal will prevents params from being added
freeze will also prevent the values from being changable
How to force a string to be a Number for a math operation?
add + in front
e.g.
let num0 = “2”
let num1 = 5
let sum = +num0 + num1
How to do positive and negative numbers in javascript?
Number.POSITIVE_INFINITY
and
Number.NEGATIVE_INFINITY
How to split an array into elements of an array
?
str.split([separator[, limit]])
How to get UTF-16 code of a string character?
str.charCodeAt(index)
How to see if a string ends or starts with certain chars?
str. startsWith(searchString[, position])
str. endsWith(searchString[, length])
How to extract a certain section of a String?
str.slice(beginIndex[, endIndex])