Standard built-in objects: Array Flashcards

1
Q

What does String.prototype.slice() do?

A

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'

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does Array.prototype.reverse() do?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the Array constructor do in JavaScript?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does Array.prototype.at() do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does Array.prototype.concat() do?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does Array.prototype.copyWithin() do?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does Array.prototype.entries() do?

A

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']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does Array.prototype.every() do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does Array.prototype.fill() do?

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does Array.prototype.filter() do?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does Array.prototype.find() do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does Array.prototype.findIndex() do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does Array.prototype.findLast() do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does Array.prototype.findLastIndex() do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does Array.prototype.flat() do?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does Array.prototype.flatMap() do?

A

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]

17
Q

What does Array.prototype.forEach() do?

A

forEach(callbackFn): Executes a provided function once for each array element.

Example: [1, 2, 3].forEach(x => console.log(x)) // logs 1, 2, 3

18
Q

What does Array.from() do?

A

from(arrayLike[, mapFn[, thisArg]]): Creates a new Array instance from an array-like or iterable object.

Example: Array.from('abc') // ['a', 'b', 'c']

19
Q

What does Array.prototype.includes() do?

A

includes(valueToFind[, fromIndex]): Determines whether an array includes a certain value among its entries.

Example: [1, 2, 3].includes(2) // true

20
Q

What does Array.prototype.indexOf() do?

A

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

21
Q

What does Array.isArray() do?

A

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

22
Q

What does Array.prototype.join() do?

A

join([separator]): Joins all elements of an array into a string and returns this string.

Example: [1, 2, 3].join('-') // '1-2-3'

23
Q

What does Array.prototype.keys() do?

A

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

24
Q

What does Array.prototype.lastIndexOf() do?

A

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

25
Q

What does Array.prototype.map() do?

A

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]

26
Q

What does Array.of() do?

A

of(...items): Creates a new Array instance from a variable number of arguments.

Example: Array.of(1, 2, 3) // [1, 2, 3]

27
Q

What does Array.prototype.pop() do?

A

pop(): Removes the last element from an array and returns that element.

Example: const arr = [1, 2, 3]; arr.pop() // 3

28
Q

What does Array.prototype.push() do?

A

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

29
Q

What does Array.prototype.reduce() do?

A

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

30
Q

What does Array.prototype.reduceRight() do?

A

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]

31
Q

What does Array.prototype.shift() do?

A

shift(): Removes the first element from an array and returns that element.

Example: const arr = [1, 2, 3]; arr.shift() // 1

32
Q

What does Array.prototype.some() do?

A

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

33
Q

What does Array.prototype.sort() do?

A

sort([compareFunction]): Sorts the elements of an array in place and returns the sorted array.

Example: [3, 1, 2].sort() // [1, 2, 3]

34
Q

What does Array.prototype.splice() do?

A

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]