Vanilla Flashcards

(30 cards)

1
Q

Difference between const, let, var and <blank>?</blank>

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to create a shallow copy from an iterable object?

A

Array.from()

  • let newArray = Array.from(someArray);*
  • let newArray = Array.from(“string is iterable in JS”);*
  • —-*—————————————————-
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to check if something is an Array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to concat Arrays?

A

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]]*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Difference between SomeType.someMethod() and SomeType.prototype.someMethod()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Difference between for..in and for..of

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A

Array.prototype.unshift()

Array.prototype.shift()

Array.prototype.push()

Array.prototype.pop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to concatenate all array elements?

A

Array.prototype.join([separator])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How find index of search query in array?

A

arr.indexOf(searchElement[, fromIndex])

will return only the first it finds with this, need to write a loop to grab all

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to filter array with a given condition

A

var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to see if an Array has a value?

A

arr.includes(valueToFind[, fromIndex])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to see if an Array has any values that pass a certain condition?

A

arr.some(callback(element[, index[, array]])[, thisArg])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to see if an Array has any values that pass a certain condition?

A

arr.every(callback(element[, index[, array]])[, thisArg])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to return a new Array with each element modified by a function

A

var new_array = arr.map(function callback(currentValue[, index[, array]]) { // Return element for new_array }[, thisArg])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to sort an array?

A

arr.sort([compareFunction])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to modify an Array in place by adding/remove/modyfing elements with given argument?

A

var arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, …]]]])

17
Q

How to select a portion of an array with a given start and possibly end index?

A

arr.slice([begin[, end]])

18
Q

How to create objects with constant params or values?

A

Object.seal(someObject) and Object.freeze(someObject)

seal will prevents params from being added

freeze will also prevent the values from being changable

19
Q

How to force a string to be a Number for a math operation?

A

add + in front

e.g.

let num0 = “2”

let num1 = 5

let sum = +num0 + num1

20
Q

How to do positive and negative numbers in javascript?

A

Number.POSITIVE_INFINITY

and

Number.NEGATIVE_INFINITY

21
Q

How to split an array into elements of an array

?

A

str.split([separator[, limit]])

22
Q

How to get UTF-16 code of a string character?

A

str.charCodeAt(index)

23
Q

How to see if a string ends or starts with certain chars?

A

str. startsWith(searchString[, position])
str. endsWith(searchString[, length])

24
Q

How to extract a certain section of a String?

A

str.slice(beginIndex[, endIndex])

25
how to all lower case or all caps a String?
str. toLowerCase() str. toUpperCase()
26
how to remove trailing and/or leading whtiespace from a string?
str. trim() str. trimEnd(); (alias: str.trimRight();) str. trimStart(); (alias: str.trimLeft();)
27
how does rest oprator work in arrays?
let a, b, c; let arr = [1, 2, 3, 4, 5, 6, 7]; // Write code here [a, b, ...c] = arr; console.log(a); // 1 console.log(b); // 2 console.log(c); // [3, 4, 5, 6, 7]
28
what is order of operations in logic gates being important, and beinga ble to assign stuff left to right with || called?
short circuiting
29
How create an object using an existing object as a prototype? (Object inheritence)
Object.create(Object.prototype) This will copy the object, althought it's properties are stored in the \_\_proto\_\_property. so the new Object won't return any of the original Object properties when calling it, only when calling those properties, e.g.: let a = {name: "John", age: "30"}; let b = a; console. log(a); // =\> { name: 'John', age: '30' } console. log(b); // =\> { name: 'John', age: '30' } b. height = 180; console. log(a); // =\> { name: 'John', age: '30', height: 180 } console. log(b); // =\> { name: 'John', age: '30', height: 180 } console. log("---------------------------"); let c = {name: "John", age: "30"}; let d = Object.create(c); console. log(c); // =\> { name: 'John', age: '30' } console. log(d); // =\> {} d. height = 180; console. log(c); // =\> { name: 'John', age: '30' } console. log(d); // =\> { height: 180 } console. log(d.\_\_proto\_\_); // =\> { name: 'John', age: '30' } console. log(d.name); // =\> John console. log(d.age); // =\> 30 console. log(d.height); // =\> 180
30