Data Types Flashcards
How are methods added to primitive data types?
- “object wrappers” are created for each primitive data type temporarily
- which provide an extra set of methods
How are “normal” numbers stored in JS?
- 64-bit format IEEE-754
- double-precision floating-point
- min is -2^53 and max is 2^53
Why does 0.1 + 0.2 equal 0.30000000000000004 in JS?
- 0.1 and 0.2 are endless fractions in binary
How can we convert values like 12pt or 100px to numbers?
- parseInt / parseFloat
How can we access characters in JS strings?
- str[index]
- str.charAt(index)
- iterable via for (let char of string)
How can we get a substring?
- slice(start, end)
- exclusive end
- allows negatives
Performance of pop/push VS. shift/unshift on arrays?
- pop/push fast
- shift/unshift slow
How to loop over elements in array?
- for (let elem of arr)
What happens if you compare (==) an array with a primitive?
- array (object) gets converted into a primitive
How can we get a subarray?
- slice(state, end)
- exclusive end
- allows negatives
How does arr.sort() behave?
- sorts array in-place
- default converts elements to strings
How can we check if value is an array?
- Array.isArray()
- because using typeof returns object
What are iterables and iterators in JS?
- basically objects that can be used in for..of loops
- must implement a method named Symbol.iterator
- result of objSymbol.iterator is an iterator
- An iterator must have the method named next() that returns an object {done: Boolean, value: any}, here done:true denotes the end of the iteration process, otherwise the value is the next value.
What are array-likes in JS?
- objects that have indexed properties and length
How do Maps and Objects differ?
- both are used for storing keyed collections
- Maps allow keys of any type, in contrast to objects where keys get converted to strings
- size property method