DEV Community

Cover image for Array methods in javascript
Kamaleshwar A
Kamaleshwar A

Posted on

Array methods in javascript

1. length

Returns the total number of elements in an array.

Example:

const fruits = ["Apple", "Banana", "Mango"];

console.log(fruits.length);
Enter fullscreen mode Exit fullscreen mode

Output:

3
Enter fullscreen mode Exit fullscreen mode

2. toString()

Converts an array into a comma-separated string.

Example:

const fruits = ["Apple", "Banana", "Mango"];

console.log(fruits.toString());
Enter fullscreen mode Exit fullscreen mode

Output:

Apple,Banana,Mango
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

Output:

Banana
Mango
Enter fullscreen mode Exit fullscreen mode

4. join()

Joins all array elements into a string using a specified separator.

Example:

const fruits = ["Apple", "Banana", "Mango"];

console.log(fruits.join(" - "));
Enter fullscreen mode Exit fullscreen mode

Output:

Apple - Banana - Mango
Enter fullscreen mode Exit fullscreen mode

5. pop()

Removes and returns the last element of the array.

Example:

const fruits = ["Apple", "Banana", "Mango"];

fruits.pop();

console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output:

["Apple", "Banana"]
Enter fullscreen mode Exit fullscreen mode

6. push()

Adds one or more elements to the end of the array.

Example:

const fruits = ["Apple", "Banana"];

fruits.push("Mango");

console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output:

["Apple", "Banana", "Mango"]
Enter fullscreen mode Exit fullscreen mode

7. shift()

Removes and returns the first element of the array.

Example:

const fruits = ["Apple", "Banana", "Mango"];

fruits.shift();

console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output:

["Banana", "Mango"]
Enter fullscreen mode Exit fullscreen mode

8. unshift()

Adds one or more elements to the beginning of the array.

Example:

const fruits = ["Banana", "Mango"];

fruits.unshift("Apple");

console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output:

["Apple", "Banana", "Mango"]
Enter fullscreen mode Exit fullscreen mode

9. Array.isArray()

Checks whether a value is an array.

Example:

const fruits = ["Apple", "Banana"];

console.log(Array.isArray(fruits));
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output:

[4, 5, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

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());
Enter fullscreen mode Exit fullscreen mode

Ouput:

[1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

Output:

["Banana", "Mango"]
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output:

["Apple", "Mango", "Orange"]
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output:

["Apple", "Mango", "Orange"]
["Apple", "Banana", "Orange"]
Enter fullscreen mode Exit fullscreen mode

Reference:

http://w3schools.com/js/js_array_methods.asp#mark_delete

Top comments (0)