L1: Collection Basics Flashcards
What is a collection
A structured container.
A place to store data.
Method that returns portions of a string or array. Does not mutate
.slice
What does String.prototype.slice() return?
returns a segment of another string.
What happens if .slice indices are out of order like (3, 1)? (for arrays and strings)
Returns an empty string or empty array
What happens with negative slice indices like (-4, -2)
Still exclusive?
Works from the end (last index is -1)
Yes still exclusive
What happens if you omit the second argument in .slice
Captured index goes till the end
What is the index for the last element?
What 2 arguments do you use to ensure you capture the last element?
-1
Not 2 arguments. not 0 or -0. Just use 1 argument
With Array.prototype.slice(), what happens if you have no arguments? What does it return? Does it return a new array or a reference to the original?
A copy of the original array
new array
What happens if you have no arguments with String.prototype.slice()?
Returns the original string
What happens when you try to reference an array/string index outside what’s there?
How about negative indices? (strings vs arrays?)as with anArray[-3]
Undefined
Undefined
Not the value undefined, but the actual absence of the value.
With arrays, -1 is viewed as a key. undefined If it’s not there, or the value if it is.
How do you distinguish between a non object key and an object key with the value undefined?
Object.prototype.hasOwnProperty()
or
Object.keys(obj).includes(‘key to test’)
What does Object.prototype.hasOwnProperty() do?
Does the argument have to be a string?
Returns boolean if an object has the argument key
No, it coerces it into a string.
What happens with Object.keys(arr) on arr = [‘foo’, ‘bar’, ‘qux’]
[ ‘0’, ‘1’, ‘2’,}
What happens with arr[-1] = 374;
It creates a key ‘-1’
Increment the value of an array element
What happens if you try to increment a non-index?
let numbers = [1, 2, 3, 4]
numbers[0] = numbers[0] + 1;
or
arr[3] += 1;
Creates a NaN in the array. Basically assigns a NaN to that index of the array.