Everything  About Array In JavaScript.

Everything About Array In JavaScript.

Table of contents

Array:

The array is a special variable that holds more than one value. It is a special kind of object in javascript that store different types of datatypes in a single variable. The array is created by declaring the square bracket or using the new keyword.

How to declare an array.
const cars = ["Honda", "Tata", "Mahindra"]
const cars = new Array("Honda", "Tata", "Mahindra");

Array Methods:

We are going to use the below example to understand the methods. let's suppose we have an array variable that stores the following values

let arr = [1, 2, 3, 4, "apple", "banana"]
Note: Whatever the output will come in the below example or whatever we modify the array we will always suppose the above array in every new example.

  1. length: It is used to check the length of an array.
    console.log(arr.length)
    output = 6

  2. push: It is used to push the element inside an array.
    arr.push("cat")
    console.log(arr.length)
    console.log(arr)
    output = 7
    output = [1, 2, 3, 4, "apple", "banana", "cat"]

  3. slice(start, end): It is used to remove a part of the element, it takes two arguments as parameter start and end position, end position is excluded. It always returns a new array, it does not modify the existing array.
    const new_arr = arr.slice(1,4)
    the first parameter defines the starting index and the second parameter defines the end index, starting from index 1 and the end value is excluded it returns the value at the end-1 index position means (4-1) 3rd index position.
    output = [2, 3, 4]

  4. splice(start, end, value): It is used to add a new element to an array.
    arr.splice(2, 1, "orange", 111)
    the first parameter (2) defines the position where new elements should be added.
    the second parameter (1) defines the position how many elements should be removed.
    rest ("orange", 111) defines the new elements to be added.
    output = [1, 2, "orange", 111, "banana"]

  5. concat: It is used to merge two existing arrays. It always creates a new array.
    let arr1 = [1, 2, 3] let arr2 = [4, 5, 6] const arr3 = arr1.concat(arr2) console.log(arr3) //output= [1, 2, 3, 4, 5, 6]

  6. fill(value, start, end): This method fills specified elements in an array with a value. It overwrites the original array. If the start and end positions are not specified then all the elements will be filled.
    arr.fill("cat", 2, 4)
    output = [1, 2, "cat", "cat", "apple", "banana"]
    arr.fill("cat")
    output = ["cat", "cat", "cat", "cat", "cat", "cat"]

  7. indexOf: It returns the first index position of elements inside an array. If the element is not found then it returns -1.
    arr.indexOf("apple") output = 4

  8. includes: Returns boolean value if the specified element is present in an array.
    Return true if the element is present and return false if the element is not present.
    arr.includes("dog") // false arr.includes("apple") // true

  9. isArray: Check whether the object is an array or not.
    Array.isArray(arr) // true Arrau.isArray(1) // false

  10. join: This method joins the array elements and returns as strings.
    arr.join(' ') // 1 2 3 4 apple banana

  11. lastIndexOf: It returns the last index position of elements inside an array. If the element is not found then it returns -1. It searches from right to left.
    let arr1 = [1, 4, 2, 5, 4, 3]
    arr.LastIndexOf(4) output = 4

  12. pop: Remove the last element from an array.
    arr.pop()
    output = banana

  13. reverse: Reverse the order of the element in an array.
    arr.reverse()
    output = ["banana", "apple", 4, 3, 2, 1]

  14. shift: Remove the first element from an array.
    arr.pop()
    output = 1

  15. push: Add a new element at the end of an array.
    arr.push("cat")
    output = [1, 2, 3, 4, "apple", "banana", "cat"]

  16. unshift: Add a new element at the start of an array.
    arr.push("cat")
    output = ["cat", 1, 2, 3, 4, "apple", "banana"]

  17. sort: It sorts the elements of an array. It overwrites the original array.
    let arr1 = [5, 6, 2, 1, 3] arr1.sort() output = [1, 2, 3, 5, 6]

  18. split: Used to convert a string into an array.
    let str = "Happy" let arr = str.split('') console.log(arr) // ['H', 'a', 'p', 'p', 'y']