JavaScript Array methods

JavaScript Array methods

JavaScript is a programming language. It is maintained by ECMA SCRIPT.

Array

An Array is a non-primitive data type, we can store different types of values inside an Array.

example:

let arr = [1, 2, 'Sudhanshu', "Modi", 8.6]

An array is enclosed within square brackets [ ].

another way of declaring an array

let arr = new Array(1, 2, 'Sudhanshu', 'Modi', 8.6)

It is not recommended because this way of declaring an array is not used by most developers.

printing on console

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
console.log(arr)

result: [1, 2, 'Sudhanshu', 'Modi', 8.6]

Accessing array elements

An array is working on index value starting from zero (0).

we can access array elements like this:

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr[2]

result: Sudhanshu

accessing the last element

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr[arr.length-1]

result: 8.6

arr.length gives the length of the array or we can say the number of elements which is inside the array.

changing value inside the array

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr[1] = 'hii'

result: [1, 'hii', 'Sudhanshu', 'Modi', 8.6]

Insertion and deletion of values inside an array

push()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr.push('Hii')

result: [1, 2, 'Sudhanshu', 'Modi', 8.6, 'Hii']

push() inserts value at the end.

pop()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr.pop()

result: [1, 2, 'Sudhanshu', 'Modi']

pop() removes the last element from an array.

shift()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr.shift()

result: [2, 'Sudhanshu', 'Modi', 8.6]

shift() removes the first element from an array.

unshift()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr.unshift(989)

result: [989, 1, 2, 'Sudhanshu', 'Modi', 8.6]

unshift() inserts value at the start.

slice()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr.slice(1, 3)

result: [2, 'Sudhanshu']

slice() returns a slice of an array.

We provide two arguments in the slice, the first argument is the starting index and the second argument is how far it goes to cut a slice of an array.

The second argument we provide is excluded means index 3 is not included.

splice()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr.splice(2, 3, 'Hii', 'Hello')

result: [1, 2, 'Hii', 'Hello']

The first argument is used from where it starts inserting values, the second argument is used for how many elements you wanted to delete or replaced inside an array and after the second value anything is provided it is used as the elements which are going to be inserted into an array.

concatenation()

It is used to concatenate two arrays.

let arr1 = [1, 2, 3]
let arr2 = [11, 22, 33]

arr1.concat(arr2)

result: [1, 2, 3, 11, 22, 33]

we can provide many arrays inside the concat() function.

fill()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]
arr.fill('Hii', 2, 4)

result: [1, 2, 'Hii', 'Hii', 8.6]

The first argument is the value we want to insert, the second argument is the index from where it starts inserting value, and the third argument is how far it can go for inserting value (it is excluded means index 4 is not included).

includes()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]

arr.includes('Sudhanshu', 2)

result: true

includes() returns a boolean value, the first argument is the value we are looking for and the second argument is the index value and it checks for value on the index which we are provided if it is true then it returns true else it returns false.

indexOf()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]

arr.indexOf('Sudhanshu')

result: 2

indexOf() gives the index of the value which we provide as an argument.

lastIndexOf

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6, 1]

arr.lastIndexOf(1)

result: 5

lastIndexOf() gives the last index of the value which we provide as an argument.

isArray()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]

Array.isArray(arr)

result: true

isArray() is used to check if a variable is an array or not.

join()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]

arr.join(_)

result: 1_2_Sudhanshu_Modi_8.6

join() is used to join an array of elements and convert it into a string, the argument we provide is used to place between elements.

We can provide any characters as an argument and it is going to join elements on that basis.

split()

let word = '1_2_Sudhanshu_Modi_8.6'

let arr = word.split(_)

result: [1, 2, 'Sudhanshu', 'Modi', 8.6]

split() is used to split a string and convert it into an array, the argument we provide is used to split characters according to it.

We can provide any characters as an argument and it is going to split characters and convert them into an array on that basis.

reverse()

let arr = [1, 2, 'Sudhanshu', 'Modi', 8.6]

arr.reverse()

result: [8.6, 'Modi', 'Sudhanshu', 2, 1]

reverse() is used to reverse elements of an array.

sort()

let word = ['ape', 'how', 'ball']

name.sort()

result: ['ape', 'ball', 'how']

sort() is used to sort elements of an array.

for of loop

let names = ['Ram', 'Shyam', 'Shiv']

for(const name of names){
    console.log(`Namaste ${name} ji`)
}

result:
    Namaste Ram ji
    Namaste Shyam ji
    Namaste Shiv ji

Working for of loop is like it iterates over an array and performs operations on it.

map()

let num = [1, 4, 9, 16, 25]

map(Math.sqrt)

result: [1, 2, 3, 4, 5]

Working of the map() is like it iterates over an array and performs operations on it and returns an array.