collections Flashcards
What are collections?
collections are made up of individual elements. In JS this includes arrays, objects, and kinda strings
Calling theslicemethod without any arguments will return…
a copy of the original string
The first argument of slice specifies…
the index at which to start the extraction
The second argument of slice specifies…
the index where you want to end the extraction. The character at the ending index isn’t part of the returned substring.
sliceandsubstringdiffer…
- When the start index is greater than the end index,
substring
swaps the two arguments whileslice
returns an empty string: - When either argument is negative,
substring
treats them as0
, while, as we saw above,slice
treats them aslength + index
: - We recommend using
String.prototype.slice
When given negative numbers as the indices,slicetreats them as
string length + index. IE, it counts backward from the last char (-1 is the last char)
String.prototype.slicereturns…
a new string
Array.prototype.slicereturns…
a new array
Callingarr.slicewithout any arguments returns…
a shallow copy of the original array
Referencing an out-of-bounds index with slice returns…
undefined
Object keys are also called…
properties
Accessing an index less than0on an array or a string returns…
undefined
to check if that property exists and simply holds undefined, or does not exist, we can use…
let obj = { a: ‘foo’, b: ‘bar’, c: undefined};
Object.keys(obj).includes(‘c’); // => true
//OR
obj.hasOwnProperty(‘c’); // => true
obj.hasOwnProperty(‘d’); // => false
Using a key to access a property that doesn’t exist on an object returns…
undefined
We can access just the keys or just the values from an object with
theObject.keysandObject.valuesmethods. These methods return an array