DEV Community

Praveen
Praveen

Posted on

Java script array methods

1*Array length*

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

Example
const country = ["India", "pakistan", "Australia", "newzealand"];

let size = country.length;

2*Javascript array tostring*
JavaScript Array toString()
The toString() method returns the elements of an array as a comma separated string.
const fruits = ["Banana", "Orange", "Apple", "Mango"];

let myList = fruits.toString();

3.JavaScript Array at()
Examples
Get the third element of fruits using at():

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

4.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:

Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
Result:

Banana * Orange * Apple * Mango

4.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");

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

Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");

6.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");
7.JavaScript Array delete()
Using delete() leaves undefined holes in the array.

Use pop() or shift() instead.

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

Top comments (0)