Top javascript Array Methods with Examples

In this blog post, we’ll be discussing the top javascript Array Methods of the javascript Array with examples. Arrays are a data structure that allow us to store multiple values in a single variable. They’re a vital part of any programming language and knowing how to work with them is essential for any developer. We’ll be covering the following methods: push(), pop(), shift(), unshift(), filter(), reverse() and sort().

What is an javascript Array Methods?

An array is a pair of square brackets [] in JavaScript. All the elements in the array are comma(,), separated.

In JavaScript, arrays can hold a collection of elements of any type.

In this tutorial, we will learn about the different data types String, Boolean, Number, Objects, and even other Arrays that can be used in JavaScript Array.

const mixedTypedArray = [100, true, 'javascript', 0);

Add Element in Array

The unshift() the method adds a new element to an array (at the beginning), and “unshifts” older elements, and returns the new length of the array.

const fruits = [ "orange", "Apple", "Mango", "Banana"];
fruits.unshift("Lemon");
console.log( fruits);
// ["Lemon","orange", "Apple", "Mango", "Banana",]

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

const fruits = ["orange", "Apple", "Mango", "Banana"];
fruits.push("Lemon");
console.log(fruits);
// ["orange", "Apple", "Mango", "Banana","Lemon"]

Delete Element

The shift() the method removes the first array element and “shifts” all other previous elements to a lower index.

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();
console.log(fruits)
// ["Orange", "Apple", "Mango"]

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

const fruits = ["Banana", "orange", "Apple", "Mango"];
fruits.pop();
console.log(fruits)
// ["Banana", "orange", "Apple"]

Filter Elements

The filter() method creates a new array filled with all array elements that pass a test provided by a function.

The filter() method does not execute the function for empty elements.

const users = [ {firstName: "Joe", lastName: "Doe"}, {firstName: "Alex", lastName: "Clay"}, {firstName: "Opie", lastName: "Winston"}, {firstName: "Wasten", lastName: "Doe"}]
const newuser = users.filter(user => user.firstName == "Opie");
console.log(newuser)

Reverse in JavaScript

The reverse() method reverses the order of the elements in an array. The first array element becomes the last, and the last array element becomes the first. The reverse() method overwrites the original array.

const array1 = ['one', 'two', 'three'];
console.log(array1);
// ["one", "two", "three"]
const reversed = array1.reverse();
console.log(reversed);
//["three", "two", "one"]

SORT in JavaScript

The sort() method sorts an array alphabetically:

const fruits = ["Banana", "orange", "Apple", "Mango"];
fruits.sort();
console.log(fruits);
//["Apple", "Banana", "Mango", "orange"]
W3TWEAKS
Latest posts by W3TWEAKS (see all)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *