The Ultimate sheet for javascript Array Methods

The Ultimate sheet for javascript Array Methods

What is an array and how to declare an array in javascript

JavaScript array is an object that represents a collection of elements. and in simple words, array is a kind of variable that can store multiple values in it

Syntex to create an array

  let words = ['python', 'java', 'javascript'];

push() - Add new element in array

You can use the built-in method push() to add elements to an array. The push() method adds an element at the end of the array. For example,

let greet = ['how', 'are'];

// add an element at the end
greet.push('you');

console.log(greet); //  ['how', 'are', 'you']

unshift() - Add element beginning of the array

The unshift() method adds an element at the beginning of the array. For example,

let greet = ['how', 'are'];

// add an element at the beginning
greet.unshift('who');

console.log(greet); //  ['who', 'are', 'you']

pop() - remove element from the array

You can use the pop() method to remove the last element from an array. The pop() method also returns the returned value. For example,

let coderLife = ['eat', 'sleep', 'code', 'repeat'];

// remove the last element
coderLife.pop();
console.log(coderLife); // ['eat', 'sleep', 'code']

shift() - remove an element from the beginning of the array

If you need to remove the first element, you can use the shift() method. The shift() method removes the first element and also returns the removed element. For example,

let coderLife = ['eat', 'sleep', 'code', 'repeat'];

// remove the first element
coderLife.shift();

console.log(coderLife); // ['sleep', 'code', 'repeat']

length - show length of the array

You can find the length of an element (the number of elements in an array) using the length property. For example,

  let coderLife = ['eat', 'sleep', 'code', 'repeat'];

// this gives the total number of elements in an array
console.log(coderLife.length); // 4

sort - It sort the array

using sort() you can sort any numeric and alphabetic array

let coderLife = ['eat', 'sleep', 'code', 'repeat'];

// sorting elements in the alphabetical order
coderLife.sort();
console.log(coderLife); // [ 'code', 'eat', 'repeat', 'sleep' ]

indexOf() - to find index of an element

indexOf() is used to find the index of a particular element from the array

let coderLife = ['eat', 'sleep', 'code', 'repeat'];

const position = coderLife.indexOf('code');
console.log(position); // 2

slice() - to cut some periocular elements from array

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end

let coderLife = ['eat', 'sleep', 'code', 'repeat'];

// slicing the array elements
const newCoderLife = coderLife.slice(2);
console.log(newCoderLife); // ['code', 'repeat' ]

concat() - use to join two arrays

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

let coderLife = ['eat', 'sleep', 'code', 'repeat'];
let newRoutine = ['game'];

// concatenating two arrays
const routine = coderLife.concat(newRoutine);
// [ 'eat', 'sleep', 'code', 'repeat', 'game' ]

map() - for iterating array

Map is a collection of elements where each element is stored as a Key, value pair. Map object can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key, value pair in the same order as inserted. map() is mostly used to iterate an array


// Array needs to be iterated
const arr = [
    {name:'rohan', subject:'javascript', roll:1},
    {name:'Biraj', subject:'Python', roll:2},
    {name:'Banglu', subject:'java', roll:3},
]

// iterateing useing map()
arr.map((obj) => {
    console.log(
        `${obj.name} is Studying ${obj.subject} and roll no is ${obj.roll}`
    )
})

// output -----------------------------------
// rohan is Studying javascript and roll no is 1
// Biraj is Studying Python and roll no is 2
// Banglu is Studying java and roll no is 3

foreach() - for iterateing array

foreach() is also used to iterateing array


// Array needs to be iterated
const arr = [
    {name:'rohan', subject:'javascript', roll:1},
    {name:'Biraj', subject:'Python', roll:2},
    {name:'Banglu', subject:'java', roll:3},
]

// iterateing useing map()
arr.forEach((obj) => {
    console.log(
        `${obj.name} is Studying ${obj.subject} and roll no is ${obj.roll}`
    )
})

// output -----------------------------------
// rohan is Studying javascript and roll no is 1
// Biraj is Studying Python and roll no is 2
// Banglu is Studying java and roll no is 3

Did you find this article valuable?

Support Rohan Malo's blog by becoming a sponsor. Any amount is appreciated!