DEV Community

Cover image for Array Methods in JavaScript
Karthick (k)
Karthick (k)

Posted on

Array Methods in JavaScript

*JavaScript Array Important Method *

To help you perform common tasks efficiently, JavaScript provides a wide variety of array methods. These methods allow you to add, remove, find, and transform array elements with ease.

JavaScript Array length

The length property of an array returns the number of elements in the array. It automatically updates as elements are added or removed.

let a = ["HTML", "CSS", "JS", "React"];
console.log(a.length);

JavaScript Array toString() Method

The toString() method converts the given value into a string with each element separated by commas.

let a = ["HTML", "CSS", "JS", "React"];
let s = a.toString();
console.log(s);

JavaScript Array join() Method

This join() method creates and returns a new string by concatenating all elements of an array. It uses a specified separator between each element in the resulting string.

let a = ["HTML", "CSS", "JS", "React"];
console.log(a.join('|'));

JavaScript Array.push() Method

The push() method is used to add an element at the end of an Array. As arrays in JavaScript are mutable objects, we can easily add or remove elements from the Array.

let a = [10, 20, 30, 40, 50];
a.push(60);
a.push(70, 80, 90);
console.log(a);

JavaScript Array.pop() Method

The pop() method is used to remove elements from the end of an array.

let a = [20, 30, 40, 50];
a.pop();
console.log(a);

JavaScript Array.shift() Method

shift() is a built-in JavaScript function that removes the first element from an array. The shift() function directly modifies the JavaScript array with which you are working. shift() returns the item you have removed from the array.

The shift() function removes the item at index position 0 and shifts the values at future index numbers down by one.

array.shift();

const array1 = [1, 2, 3];

const firstElement = array1.shift();

console.log(array1);
// expected output: Array [2, 3]

console.log(firstElement);
// expected output: 1`

JavaScript Array map() Method

The map() method creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method.

`let a = [4, 9, 16, 25];
let sub = a.map(geeks);

function geeks() {
return a.map(Math.sqrt);
}
console.log(sub);`


What is the output of the following code?

let a = ["HTML", "CSS", "JS", "React"];

console.log(a.length);

Tell me Answer===>Command me

Top comments (1)

Collapse
 
madhanraj profile image
Madhan Raj

answer = 4