DEV Community

vishwa v
vishwa v

Posted on

Array(Methods)

JavaScript Array length
The length property returns the length (size) of an array:

Example:

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

let size = fruits.length;

fruits.length = 2;
Enter fullscreen mode Exit fullscreen mode

JavaScript Array toString()
The toString() method returns the elements of an array as a comma separated string.

Example:

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

let myList = fruits.toString();
Enter fullscreen mode Exit fullscreen mode

Every JavaScript object has a toString() method.

JavaScript Array at()
ES2022 introduced the array method at():

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits.at(2);
Enter fullscreen mode Exit fullscreen mode

Many languages allow negative bracket indexing like [-1] to access elements from the end of an object / array / string.The at() method was introduced in ES2022 to solve this problem.

JavaScript Array join()
The join() method also joins all array elements into a string.

It behaves just like toString(), but in addition you can specify the separator:

const fruits=["apple","mango","grape"];
        // let fruit=fruits.join("*")
console.log(fruits.join("+"));
Enter fullscreen mode Exit fullscreen mode

JavaScript Array pop()
The pop() method removes the last element from an array:

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();
Enter fullscreen mode Exit fullscreen mode

JavaScript Array push()
The push() method adds a new element to an array (at the end):

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
Enter fullscreen mode Exit fullscreen mode

JavaScript Array shift()
The shift() method removes the first array element and "shifts" all other elements to a lower index.

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();
Enter fullscreen mode Exit fullscreen mode

JavaScript Array unshift()
The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon");
Enter fullscreen mode Exit fullscreen mode

Array.isArray()
ECMAScript 5 (JavaScript 2009) added the new method Array.isArray() to JavaScript:

Example
Array.isArray(fruits);

JavaScript Array delete()
Warning !
Using delete() leaves undefined holes in the array.

Use pop() or shift() instead.

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0];
Enter fullscreen mode Exit fullscreen mode

JavaScript Array concat()
The concat() method creates a new array by merging (concatenating) existing arrays:

Example (Merging Two Arrays)

const myGirls = ["Cecilie", "Lone"];
const myBoys = ["Emil", "Tobias", "Linus"];

const myChildren = myGirls.concat(myBoys);
Enter fullscreen mode Exit fullscreen mode

Array copyWithin()
The copyWithin() method copies array elements to another position in an array:

Examples
Copy to index 2, all elements from index 0:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
Enter fullscreen mode Exit fullscreen mode

JavaScript Array flat()
ES2019 Introduced the Array flat() method.

The flat() method creates a new array with sub-array elements concatenated to a specified depth.

Example

const myArr = [[1,2],[3,4],[5,6]];
const newArr = myArr.flat();
Enter fullscreen mode Exit fullscreen mode

JavaScript Array flatMap()
ES2019 added the Array flatMap() method to JavaScript.

The flatMap() method first maps all elements of an array and then creates a new array by flattening the array.

Example

const myArr = [1, 2, 3, 4, 5, 6];
const newArr = myArr.flatMap(x => [x, x * 10]);
Enter fullscreen mode Exit fullscreen mode

JavaScript Array splice()
The splice() method can be used to add new items to an array:

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
Enter fullscreen mode Exit fullscreen mode

The first parameter (2) defines the position where new elements should be added (spliced in).

The second parameter (0) defines how many elements should be removed.

JavaScript Array slice()
The slice() method slices out a piece of an array into a new array:

Example
Slice out a part of an array starting from array element 1 ("Orange"):

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1);
Enter fullscreen mode Exit fullscreen mode

The slice() method creates a new array.

The slice() method does not remove any elements from the source array.

Top comments (0)