Data Types Flashcards
What is the result of this: const s = [5, 6, 7]; s = [1, 2, 3]; // ???? s[2] = 45; // ?????? console.log(s); // ?????
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.
What will let math = 12 + ‘13’; console.log to?
1213
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY']; let arr2;
arr2 = arr1;
arr1[0] = ‘OCT’
console.log(arr2); /// what does this log?
[ ‘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]
What does Array.shift() do?
Removes the first item from an array
let array = [‘today’, ‘was’, ‘not’, ‘so’, ‘great’];
let splice = array.splice(2, 2);
What are the values of array and splice?
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))
const arr = ['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms'] const newArr = arr.slice(2, 4) What is newArray?
[ ‘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
What is a rest operator? What is a spread operator? What is the difference between the two?
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
What does Array.from() do and when would you use it?
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.
“Special numeric values” including Infinity, -Infinity, and NaN fall into the “Number” data type. True or False
True!
Name the differences between Number() and parseInt()
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
What is a symbol and how do you create one?
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 can you compare values like ===, but that’ll work with NaN or 0 & -0?
Object.is(NaN, NaN) === true
Object.is(0, -0) === false
What is the result of this:
console.log( ‘z’ > ‘Z’ );
console.log( ‘z’ > ‘Z’ ); //true
What is a Map? How is it different than a regular object?
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).
Name some property and methods of Map.
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.