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]
What does Array.prototype.flatMap()
do?
flatMap(callbackFn)
: Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.
Example: [1, 2, 3].flatMap(x => [x, x * 2]) // [1, 2, 2, 4, 3, 6]
What does Array.prototype.forEach()
do?
forEach(callbackFn)
: Executes a provided function once for each array element.
Example: [1, 2, 3].forEach(x => console.log(x)) // logs 1, 2, 3
What does Array.from()
do?
from(arrayLike[, mapFn[, thisArg]])
: Creates a new Array instance from an array-like or iterable object.
Example: Array.from('abc') // ['a', 'b', 'c']
What does Array.prototype.includes()
do?
includes(valueToFind[, fromIndex])
: Determines whether an array includes a certain value among its entries.
Example: [1, 2, 3].includes(2) // true
What does Array.prototype.indexOf()
do?
indexOf(searchElement[, fromIndex])
: Returns the first index at which a given element can be found in the array, or -1 if it is not present.
Example: [1, 2, 3].indexOf(2) // 1
What does Array.isArray()
do?
indexOf(searchElement[, fromIndex])
: Returns the first index at which a given element can be found in the array, or -1 if it is not present.
Example: [1, 2, 3].indexOf(2) // 1
What does Array.prototype.join()
do?
join([separator])
: Joins all elements of an array into a string and returns this string.
Example: [1, 2, 3].join('-') // '1-2-3'
What does Array.prototype.keys()
do?
keys()
: Returns a new Array Iterator object that contains the keys for each index in the array.
Example: const iterator = [1, 2, 3].keys(); iterator.next().value // 0
What does Array.prototype.lastIndexOf()
do?
lastIndexOf(searchElement[, fromIndex])
: Returns the last index at which a given element can be found in the array, or -1 if it is not present.
Example: [1, 2, 3, 2].lastIndexOf(2) // 3
What does Array.prototype.map()
do?
map(callbackFn)
: Creates a new array populated with the results of calling a provided function on every element in the calling array.
Example: [1, 2, 3].map(x => x * 2) // [2, 4, 6]
What does Array.of()
do?
of(...items)
: Creates a new Array instance from a variable number of arguments.
Example: Array.of(1, 2, 3) // [1, 2, 3]
What does Array.prototype.pop()
do?
pop()
: Removes the last element from an array and returns that element.
Example: const arr = [1, 2, 3]; arr.pop() // 3
What does Array.prototype.push()
do?
push(...items)
: Adds one or more elements to the end of an array and returns the new length.
Example: const arr = [1, 2]; arr.push(3) // 3
What does Array.prototype.reduce()
do?
reduce(callbackFn[, initialValue])
: Executes a reducer function on each element of the array, resulting in a single output value.
Example: [1, 2, 3].reduce((acc, val) => acc + val, 0) // 6
What does Array.prototype.reduceRight()
do?
reduceRight(callbackFn[, initialValue])
: Applies a function against an accumulator and each element in the array (from right-to-left) to reduce it to a single value.
Example: [[0, 1], [2, 3], [4, 5]].reduceRight((acc, val) => acc.concat(val)) // [4, 5, 2, 3, 0, 1]
What does Array.prototype.shift()
do?
shift()
: Removes the first element from an array and returns that element.
Example: const arr = [1, 2, 3]; arr.shift() // 1
What does Array.prototype.some()
do?
some(callbackFn)
: Tests whether at least one element in the array passes the test implemented by the provided function.
Example: [1, 2, 3].some(x => x > 2) // true
What does Array.prototype.sort()
do?
sort([compareFunction])
: Sorts the elements of an array in place and returns the sorted array.
Example: [3, 1, 2].sort() // [1, 2, 3]
What does Array.prototype.splice()
do?
splice(start[, deleteCount[, ...items]])
: Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Example: const arr = [1, 2, 3]; arr.splice(1, 1, 'a') // [1, 'a', 3]