Standard built-in objects: Array Flashcards
What does String.prototype.slice()
do?
slice(start, end)
: Extracts part of a string from start
index up to, but not including, the end
index. Negative indices count from the end.
Example: "Hello".slice(1, 4) // 'ell'
What does Array.prototype.reverse()
do?
reverse()
: Reverses the elements of an array in place (mutates the array). The first array element becomes the last and the last becomes the first.
Example: [1, 2, 3].reverse() // [3, 2, 1]
What does the Array
constructor do in JavaScript?
The Array
constructor creates a new array object. It can be invoked in multiple ways:
- Array()
: Creates an empty array.
- Array(length)
: Creates an array with a specified length, but no elements.
- Array(element0, element1, ..., elementN)
: Creates an array with the given elements.
Example: new Array(3) // [undefined, undefined, undefined]
Example: new Array(1, 2, 3) // [1, 2, 3]
What does Array.prototype.at()
do?
at(index)
: Returns the array element at the given index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
Example: [1, 2, 3].at(0) // 1
Example: [1, 2, 3].at(-1) // 3
What does Array.prototype.concat()
do?
concat(value1, value2, ..., valueN)
: This is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
Example: [1, 2, 3].concat([4, 5], 6) // [1, 2, 3, 4, 5, 6]
What does Array.prototype.copyWithin()
do?
copyWithin(target, start, end)
: Shallow copies part of an array to another location in the same array and returns it, without modifying its length.
Example: [1, 2, 3, 4, 5].copyWithin(0, 3) // [4, 5, 3, 4, 5]
What does Array.prototype.entries()
do?
entries()
: Returns a new Array Iterator object that contains the key/value pairs for each index in the array.
Example:
const iterator = ['a', 'b', 'c'].entries(); iterator.next().value // [0, 'a']
What does Array.prototype.every()
do?
every(callbackFn)
: Tests whether all elements in the array pass the test implemented by the provided callback function.
Example: [1, 2, 3].every(x => x > 0) // true
What does Array.prototype.fill()
do?
fill(value, start, end)
: Fills all the elements of an array from a start index to an end index with a static value.
Example:
[1, 2, 3].fill(4) // [4, 4, 4] [1, 2, 3, 4].fill(9, 2, 4) // [1, 2, 9, 9]
What does Array.prototype.filter()
do?
filter(callbackFn)
: Creates a new array with all elements that pass the test implemented by the provided function.
Example: [1, 2, 3].filter(x => x > 1) // [2, 3]
What does Array.prototype.find()
do?
find(callbackFn)
: Returns the value of the first element in the array that satisfies the provided testing function.
Example: [1, 2, 3].find(x => x > 1) // 2
What does Array.prototype.findIndex()
do?
findIndex(callbackFn)
: Returns the index of the first element in the array that satisfies the provided testing function.
Example: [1, 2, 3].findIndex(x => x > 1) // 1
What does Array.prototype.findLast()
do?
findLast(callbackFn)
: Returns the value of the last element in the array that satisfies the provided testing function.
Example: [1, 2, 3, 2].findLast(x => x === 2) // 2
What does Array.prototype.findLastIndex()
do?
findLastIndex(callbackFn)
: Returns the index of the last element in the array that satisfies the provided testing function.
Example: [1, 2, 3, 2].findLastIndex(x => x === 2) // 3
What does Array.prototype.flat()
do?
flat(depth)
: Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Example: [[1, 2], [3, 4]].flat() // [1, 2, 3, 4]