Array Methods in JavaScript
Declaration and Initialization
Array Declaration
- Using array literal:
var arr = [];
- Using
Array.of()
(ES6):var arr = Array.of(1, 2, 3);
- Using constructor:
var arr = new Array(3); // Creates an array with 3 empty slots
Array Properties and Methods
length
- Description: Returns or sets the number of elements in an array.
- Example:
arr.length = 5; // Resizes the array
push()
- Description: Adds one or more elements to the end of the array.
- Example:
arr.push(4, 5);
pop()
- Description: Removes the last element of the array.
- Example:
arr.pop();
shift()
- Description: Removes the first element of the array.
- Example:
arr.shift();
unshift()
- Description: Adds one or more elements to the beginning of the array.
- Example:
arr.unshift(0);
reverse()
- Description: Reverses the order of the elements in the array.
- Example:
arr.reverse();
concat()
- Description: Concatenates two or more arrays.
- Example:
var newArr = arr.concat([4, 5]);
includes()
(ES6)
- Description: Checks if the array includes a specific element.
- Example:
arr.includes(2);
indexOf()
- Description: Returns the first index of the specified element, or
-1
if not found. - Example:
arr.indexOf(2);
lastIndexOf()
- Description: Returns the last index of the specified element.
- Example:
arr.lastIndexOf(2);
splice(position, n)
- Description: Adds or removes elements from an array at a specified position.
- Example:
arr.splice(1, 2); // Removes 2 elements starting at index 1
slice(start, end)
- Description: Returns a shallow copy of a portion of an array without modifying the original.
- Example:
arr.slice(1, 3);
join()
- Description: Joins all elements of an array into a string.
- Example:
arr.join(", ");
copyWithin(from, start, end)
(ES6)
- Description: Copies a sequence of elements within the array to another position.
- Example:
arr.copyWithin(0, 2, 4);
fill(value, start, end)
(ES6)
- Description: Fills all elements in an array with a static value.
- Example:
arr.fill(0, 1, 3);
flat()
- Description: Flattens nested arrays.
- Example:
let nestedArr = [1, [2, [3, 4]]];
nestedArr.flat(2); // [1, 2, 3, 4]
Static Functions
Array.from()
(ES6)
- Description: Creates an array from an iterable or array-like object.
- Example:
Array.from("hello"); // ["h", "e", "l", "l", "o"]
Array.isArray()
- Description: Checks if the provided value is an array.
- Example:
Array.isArray(arr); // true
Array.of()
(ES6)
- Description: Creates an array with the specified elements.
- Example:
Array.of(1, 2, 3);
Iteration Methods
entries()
(ES6)
- Description: Creates an iterator object with key-value pairs.
- Example:
let iterator = arr.entries();
console.log(iterator.next().value); // [0, 1]
keys()
(ES6)
- Description: Creates an iterator for the array keys.
- Example:
let keys = arr.keys();
console.log(keys.next().value); // 0
values()
(ES6)
- Description: Creates an iterator for the array values.
- Example:
let values = arr.values();
console.log(values.next().value); // 1
every()
- Description: Checks if every element satisfies a condition.
- Example:
arr.every((x) => x > 0);
some()
- Description: Checks if at least one element satisfies a condition.
- Example:
arr.some((x) => x > 5);
filter()
- Description: Creates a new array with elements that satisfy a condition.
- Example:
arr.filter((x) => x > 2);
find()
(ES6)
- Description: Returns the first element that satisfies a condition.
- Example:
arr.find((x) => x > 2);
findIndex()
(ES6)
- Description: Returns the index of the first element that satisfies a condition.
- Example:
arr.findIndex((x) => x > 2);
forEach()
- Description: Executes a provided function for each array element.
- Example:
arr.forEach((x) => console.log(x));
map()
- Description: Creates a new array by applying a function to each element.
- Example:
arr.map((x) => x * 2);
reduce()
- Description: Applies a function to reduce the array to a single value.
- Example:
arr.reduce((acc, x) => acc + x, 0);
reduceRight()
- Description: Similar to
reduce()
, but operates from right to left. - Example:
arr.reduceRight((acc, x) => acc + x, 0);