Paramclasses Academy - Blog

Blog Details

What is an Array method in Java script ?

What is an Array method in Java script ?

October 16, 2024

Array Properties and Methods

JavaScript arrays come with several built-in properties and methods that you can use:

1. Length Property

You can find the number of elements in an array using the length property.

let fruits = ["apple", "banana", "cherry"]; console.log(fruits.length); 

// Output: 3 

2. Push Method

Adds one or more elements to the end of an array and returns the new length of the array.

let fruits = ["apple", "banana"]; fruits.push("cherry"); console.log(fruits); 

// Output: ["apple", "banana", "cherry"] 

3. Pop Method

Removes the last element from an array and returns that element.

let fruits = ["apple", "banana", "cherry"]; let lastFruit = fruits.pop(); console.log(lastFruit);

// Output: cherry console.log(fruits); // Output: ["apple", "banana"]

4. Shift Method

Removes the first element from an array and returns that element.

let fruits = ["apple", "banana", "cherry"]; let firstFruit = fruits.shift(); console.log(firstFruit); // Output: apple console.log(fruits); 

// Output: ["banana", "cherry"] 

5. Unshift Method

Adds one or more elements to the beginning of an array and returns the new length of the array.

let fruits = ["banana", "cherry"]; fruits.unshift("apple"); console.log(fruits);

// Output: ["apple", "banana", "cherry"]

6. Slice Method

Returns a shallow copy of a portion of an array into a new array.

let fruits = ["apple", "banana", "cherry", "date"]; let citrus = fruits.slice(1, 3); console.log(citrus);

// Output: ["banana", "cherry"]

7. Splice Method

Adds or removes elements from an array.

let fruits = ["apple", "banana", "cherry"]; fruits.splice(1, 1, "blueberry");

// Removes 1 element at index 1 and adds "blueberry" console.log(fruits); // Output: ["apple", "blueberry", "cherry"]

Copyright © Paramwebinfo Academy.All Rights Reserved

Designed by PARAMWEBINFO