1. length
Returns the total number of elements in an array.
Example:
const fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.length);
Output:
3
2. toString()
Converts an array into a comma-separated string.
Example:
const fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.toString());
Output:
Apple,Banana,Mango
3. at()
Returns the element at the specified index. It also supports negative indexes.
Example:
const fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.at(1));
console.log(fruits.at(-1));
Output:
Banana
Mango
4. join()
Joins all array elements into a string using a specified separator.
Example:
const fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.join(" - "));
Output:
Apple - Banana - Mango
5. pop()
Removes and returns the last element of the array.
Example:
const fruits = ["Apple", "Banana", "Mango"];
fruits.pop();
console.log(fruits);
Output:
["Apple", "Banana"]
6. push()
Adds one or more elements to the end of the array.
Example:
const fruits = ["Apple", "Banana"];
fruits.push("Mango");
console.log(fruits);
Output:
["Apple", "Banana", "Mango"]
7. shift()
Removes and returns the first element of the array.
Example:
const fruits = ["Apple", "Banana", "Mango"];
fruits.shift();
console.log(fruits);
Output:
["Banana", "Mango"]
8. unshift()
Adds one or more elements to the beginning of the array.
Example:
const fruits = ["Banana", "Mango"];
fruits.unshift("Apple");
console.log(fruits);
Output:
["Apple", "Banana", "Mango"]
9. Array.isArray()
Checks whether a value is an array.
Example:
const fruits = ["Apple", "Banana"];
console.log(Array.isArray(fruits));
Output:
true
10. concat()
Combines two or more arrays and returns a new array.
Example:
const arr1 = [1, 2];
const arr2 = [3, 4];
const result = arr1.concat(arr2);
console.log(result);
Output:
[1, 2, 3, 4]
11. copyWithin()
Definition:
Copies part of an array to another position in the same array.
Example:
const numbers = [1, 2, 3, 4, 5];
numbers.copyWithin(0, 3);
console.log(numbers);
Output:
[4, 5, 3, 4, 5]
Explanation:
- Start copying from index 3 → [4, 5]
- Paste starting at index 0
12. flat()
Flattens nested arrays into a single array.
Example:
const arr = [1, 2, [3, 4], [5, 6]];
console.log(arr.flat());
Ouput:
[1, 2, 3, 4, 5, 6]
13. slice()
Returns a portion of an array without changing the original array.
Example:
const fruits = ["Apple", "Banana", "Mango", "Orange"];
console.log(fruits.slice(1, 3));
Output:
["Banana", "Mango"]
14. splice()
Adds, removes, or replaces elements in an array. It modifies the original array.
Example:
const fruits = ["Apple", "Banana", "Orange"];
fruits.splice(1, 1, "Mango");
console.log(fruits);
Output:
["Apple", "Mango", "Orange"]
Explanation:
- Start at index 1
- Remove 1 element (Banana)
- Insert "Mango"
15. toSpliced()
Works like splice(), but returns a new array instead of modifying the original array.
Example:
const fruits = ["Apple", "Banana", "Orange"];
const newFruits = fruits.toSpliced(1, 1, "Mango");
console.log(newFruits);
console.log(fruits);
Output:
["Apple", "Mango", "Orange"]
["Apple", "Banana", "Orange"]
Top comments (0)