Data Types Flashcards

1
Q
What is the result of this:
const s = [5, 6, 7];
s = [1, 2, 3]; // ????
s[2] = 45; // ??????
console.log(s); // ?????
A

s = [1, 2, 3]; // throws error, trying to assign a const
s[2] = 45; // works just as it would with an array declared with var or let
console.log(s); // returns [5, 6, 45]

Objects (including arrays and functions) assigned to a variable using const are still mutable. Using the const declaration only prevents reassignment of the variable identifier.

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

What will let math = 12 + ‘13’; console.log to?

A

1213

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;

arr2 = arr1;

arr1[0] = ‘OCT’
console.log(arr2); /// what does this log?

A

[ ‘OCT’, ‘FEB’, ‘MAR’, ‘APR’, ‘MAY’ ]

arr1 and arr2 are referencing the same array (object), so when arr1 is update, arr2 is updated.

To make an array that won’t change with arr1, use the spread operator:
arr2 = […arr1]

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

What does Array.shift() do?

A

Removes the first item from an array

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

let array = [‘today’, ‘was’, ‘not’, ‘so’, ‘great’];
let splice = array.splice(2, 2);
What are the values of array and splice?

A

array now equals [‘today’, ‘was’, ‘great’]
splice equals [‘not’, ‘so’]
(removes 2 elements beginning with the 2nd index)

Array.splice(starting index, how many to remove, (optional items to add in place))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
const arr = ['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']
const newArr = arr.slice(2, 4)
What is newArray?
A

[ ‘warm’, ‘sunny’ ]
arr.slice() returns a new array and takes in up to 2 arguments- first the starting index and second the ending index (not included).
arr will return the same array as it is not affected

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

What is a rest operator? What is a spread operator? What is the difference between the two?

A

Rest operator gathers/collects items. It can be used in destructuring items and must be placed last. Spread operator allows an iterable (such as an array) to spread/expand individually inside receiver. It spits items into single items and copies them. Use rest when declaring functions and spread when invoking the function

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

What does Array.from() do and when would you use it?

A

Returns Array object from an object with a length property or an iterable object. Turns array-like into array -string, nodeList, Set. For example, if you use a querySelectorAll to grab a nodeList, using Array.from() you can then use array methods on the list.

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

“Special numeric values” including Infinity, -Infinity, and NaN fall into the “Number” data type. True or False

A

True!

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

Name the differences between Number() and parseInt()

A

Number() converts the type whereas parseInt() parses the value of input. parseInt will parse up to the first non-digit character.

parseInt(‘32px’); // 32
Number(‘32px’) // NaN

parseInt(‘5e1’); //5
Number(‘5e1’); //50

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

What is a symbol and how do you create one?

A
A “symbol” represents a unique identifier. 
// id is a new symbol
let id = Symbol();
Upon creation, we can give symbol a description (also called a symbol name), mostly useful for debugging purposes:
// id is a symbol with the description "id"
let id = Symbol("id");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you compare values like ===, but that’ll work with NaN or 0 & -0?

A

Object.is(NaN, NaN) === true

Object.is(0, -0) === false

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

What is the result of this:

console.log( ‘z’ > ‘Z’ );

A

console.log( ‘z’ > ‘Z’ ); //true

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

What is a Map? How is it different than a regular object?

A

Map – is a collection of keyed values.

Map allows keys of any type, including objects. Keys are not converted into strings.

Insertion order is used in Map. The iteration goes in the same order as the values were inserted. Map preserves this order, unlike a regular Object (although we can’t reorder elements or directly get an element by its number).

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

Name some property and methods of Map.

A

new Map() – creates the map.

map. set(key, value) – stores the value by the key.
map. get(key) – returns the value by the key, undefined if key doesn’t exist in map.
map. has(key) – returns true if the key exists, false otherwise.
map. delete(key) – removes the value by the key.
map. clear() – removes everything from the map.
map. size – returns the current element count.

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

What is a Set? Name some property and methods of Set.

A

A Set is a special type collection – “set of values” (without keys), where each value may occur only once.

new Set(iterable) – creates the set, and if an iterable object is provided (usually an array), copies values from it into the set.

set. add(value) – adds a value, returns the set itself.
set. delete(value) – removes the value, returns true if value existed at the moment of the call, otherwise false.
set. has(value) – returns true if the value exists in the set, otherwise false.
set. clear() – removes everything from the set.
set. size – is the elements count.

17
Q

What array methods modify the array itself?

A

sort, reverse, splice

18
Q

What are the functional array methods?

A

map, filter, and reduce.

19
Q

What is the map array method?

A

The map method performs an action on every item in an array, but creates a new array to store the results.

array.map((currentValue, index, arr) => { })

20
Q

What array method allows you to undo array nesting?

A

flat()

Example:
var nestedArr = [1, 2, [3, 4, [5, 6]]];
nestedArr.flat();
console.log(nestedArr)
// expected output: [1, 2, 3, 4, [5, 6]]

var moreNested = [1, 2, [3, 4, [5, 6]]];
moreNested.flat(2);
console.log(moreNested)
// expected output: [1, 2, 3, 4, 5, 6]

21
Q

What does the find array method do?

A

The find() method is an Array.prototype (aka built-in) method which takes in a callback function and calls that function for every item it iterates over inside of the array it is bound to.

When it finds a match (in other words, the callback function returns true), the method returns that particular array item and immediately breaks the loop. So the find() method returns the first element inside an array which satisfies the callback function.

Syntax:
Arrow function- find((element, index, array) => { /* … */ } )

Inline call- find(function(element, index, array) { /* … */ }, thisArg)
With inline call, can have parameter “thisArg”, which is an object to use as this in callback function. (Can’t use in arrow function because it doesn’t bind their own this

22
Q

What does this includes method do?

A

includes is most useful when are looking for the existence of a specific value. All you have to do is provide the value you are looking for, and includes will return true if it finds it and false if it does not.

includes(searchElement, fromIndex)

23
Q

Explain how primitive data types are immutable.

A

Primitives have no methods but still behave as if they do. When properties are accessed on primitives, JavaScript auto-boxes the value into a wrapper object and accesses the property on that object instead. For example, “foo”.includes(“f”) implicitly creates a String wrapper object and calls String.prototype.includes() on that object. This auto-boxing behavior is not observable in JavaScript code but is a good mental model of various behaviors — for example, why “mutating” primitives does not work (because str.foo = 1 is not assigning to the property foo of str itself, but to an ephemeral wrapper object).

24
Q

What is

1) typeof Object?
2) typeof Object.prototype?

A
1) function! This is an object constructor that helps create objects.
const obj = {} is using the Object constructor under the hood to create that object.
Typeof obj would return object.
2) object. This is the base object