Array methods in JavaScript !

Array methods in JavaScript !

Introduction to array methods in javascript.

Method

Methods are built-in functions in the language in this case javascript that are used to ease out certain tasks.

Array

Arrays are collections of multiple elements of different datatypes that are grouped inside the opening and closing square brackets []. They come under Non-Primitive Datatypes meaning they can store a large number of data at the same time.

Let's learn about different methods in Array.

push(): This method adds an element at the end of the array.

Example:

let arr1 = [1, 4, 9, 16, 25]
console.log('Before Push: ', arr1)
arr1.push(100) 
console.log('After Push: ', arr1)

Output:

pop(): This method removes an element from the end.

Example:

let arr1 = [1, 4, 9, 16, 25]
console.log('Before Pop: ', arr1)
console.log('Pop: ', arr1.pop())  
console.log('After Pop: ', arr1)

Output:

unshift (): This method adds an element at the beginning of an array.

Example:

let arr = [1, 2, 3, 4, 5]
console.log('Before Unshift: ', arr)
arr.unshift(7)
console.log('After Unshift: ', arr)

Output:

shift(): This method removes an element from the beginning of an array.

Example:

let arr = [1, 2, 3, 4, 5]
console.log('Before Shift: ', arr)
arr.shift()
console.log('After Shift: ', arr)

Output:

slice(): This method is used when you want to select elements from a certain point to a certain point. It takes two parameters starting index point and an ending index point. Note here the ending index point is not included.

Example:

let fruits = ['apple', 'mango', 'orange', 'chiku', 'blueberry']
console.log('Fruits: ', fruits)
console.log('Sliced Fruits: ', fruits.slice(0, 3))

Output:

splice(): This method will replace/remove the element with the new element specified. It takes three parameters starting index point, the number of elements to be removed and the element.

Example:

let games = ['football', 'cricket', 'badminton', 'volleyball', 'boxing', 'shotput']
console.log('Games: ', games)
games.splice(2, 2, 'swimming')
console.log('Spliced Games: ', games)

Output:

concat(): It is a concatenation method used to concatenate the two arrays.

Example:

let arr1 = [1, 2, 3, 4, 5]
let arr2 = [6, 7, 8, 9, 10]
let arr3 = [6, 7, 6, 7, 8]
console.log('Before Concat: ', arr1)
console.log('After Concat: ', arr1.concat(arr2))

Output:

fill(): This method fills the array elements with the new elements specified by replacing or removing the elements on that particular index. It takes 3 parameters element, starting index point and an ending index point which is excluded.

Example:

let arr4 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log('Before Fill: ', arr4)
arr4.fill('onkar', 2, 4) 
console.log('After Fill: ', arr4)

Output:

include(): This method is used to check if an element exists in the array and returns true if exists else returns false. It takes single and two parameters. A single parameter is used if you want to check elements in the entire array from start to end. Two parameters are used if you want to check an element from a certain point onwards, it includes parameters like search element and index number from which you want to begin the search.

Example:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8]
console.log('Numbers: ', numbers)
console.log('Include 7 from 6th index ?: ', numbers.includes(7,6)) 
console.log('Include 9 ?: ', numbers.includes(9))

Output:

indexOf(): This method is used to check the index of an element. It returns the index number of an element if the element exists else returns -1.

Example:

let numbers = [1, 2, 3, 4, 5, 3, 6, 7, 8]
console.log('Numbers: ', numbers)
console.log('Index of 4: ', numbers.indexOf(4))
console.log('Index of 9: ', numbers.indexOf(9))
console.log('Index of 3: ', numbers.indexOf(3))

Output:

isArray(): This method checks if the variable is an array or not. It returns boolean values like true if it is an array else returns false.

Example:

let number = 10
let numbers = [1, 2, 3, 4, 5, 3, 6, 7, 8]
let numbers2 = { 1: 1, 2: 2 }
console.log('isArray: ', Array.isArray(number))
console.log('isArray: ', Array.isArray(numbers))
console.log('isArray: ', Array.isArray(numbers2))

Output:

join(): This method is used to join an element next to every element in the array.

Example:

let arr1 = [1, 2, 3, 4, 5]
let arr2 = [6, 7, 8, 9]
console.log('arr1 before join: ', arr1)
console.log('after join: ', arr1.join(' and '))
console.log('arr2 before join: ', arr2)
console.log('after join: ', arr2.join(1))

Output:

lastIndexOf(): This method returns the last index of a particular element in the array. If there are duplicate elements in the array it will return the last index of the duplicate element which is at the end of the array. It takes a parameter as an element name.

Example:

let arr1 = [1, 2, 3, 4, 5, 3, 6, 7, 8, 'onkar']
console.log('Array : ', arr1)
console.log(`Last index of onkar : ${arr1.lastIndexOf('onkar')}`)
console.log(`Last index of 3 : ${arr1.lastIndexOf(3)}`)

Output:

reverse(): This method is used to reverse the order of an array.

Example:

let arr = [1, 2, 3, 4, 5]
console.log('Before reverse array: ', arr)
console.log('After reverse array: ', arr.reverse())

Output:

sort(): This method is used to sort the array in ascending order by default.

Example:

let arr = [1, 2, 3, 8, 7, 6]
let arrNames = ['onkar', 'anurag', 'bibek', 'manali']
console.log('Before sort arr: ', arr)
console.log('After sort: ', arr.sort())
console.log('Before sort names: ', arrNames)
console.log('After sort: ', arrNames.sort())

Output:

split(): This method is used to convert a non-array to an array. It takes a single parameter.

Example:

let name = 'onkar'
console.log('Before split: ', name)
let arr1 = name.split('')
console.log('After split: ', arr1)

Output:

map(): This method is used to iterate over every element in an array and perform operations accordingly in the below exam we are iterating over each element in the array and returning the square root of it.

Example:

let arr1 = [1, 4, 9, 16, 25]
console.log('Array: ', arr1)
console.log('After Map: ', arr1.map(Math.sqrt))

Output: