Array in JavaScript

  • The array is a type of object.

  • The array is the collection of data.

  • You can store any data any other data types of data in it.

  • Suppose, we have data in the list, then we can use Array to store it.

Syntax

// Square bracket to define an array in javaScript

const array = []

Example: 

const users = ["Rohan", "Sohan", "Shubham", "Radhe", "Raju", "Abhishek"]

// You can access each users name by their index
// Index is the position of the element
// Index starts from zero
// The array length property is not about total element in the array. Actually, it is index + 1

let myFriends = [];
myFriends[50] = 'Raju';

console.log(myFriends.length); // 51

// Lenght would count from  one

//**One more syntax to create an array. It exactly works as same.**

const array = new Array ("Mohan", "Sohan", "Sandeep")

console.log(array) //["Mohan", "Sohan", "Sandeep"]
 

Examples:

//Access array elements by their index
 
const users = ["Rohan", "Sohan", "Shubham", "Radhe", "Raju", "Abhishek"]

 console.log(users[0]) // rohan
 console.log(users[4]) //raju
 console.log(users[3]) // Radhe

// Total length of an array 

console.log(users.length) // 6

//Last element of an array

console.log(users[users.length - 1]); // Abhishek

// Get the Last element by using at()

const lastName = ['Rohan', 'Sohan', 'Shubham', 'Radhe', 'Raju', 'Abhishek'];

console.log(lastName.at(-1))

Replace array element using array index

const users = ['Rohan', 'Sohan', 'Shubham', 'Radhe', 'Raju', 'Abhishek'];

users[0] = 'Pritam';

console.log(users); // ["Pritam", "Sohan", "Shubham", "Radhe", "Raju", "Abhishek"]

Add element using array index

const users = ['Rohan', 'Sohan', 'Shubham', 'Radhe', 'Raju', 'Abhishek'];

users[6] = 'Pritam';

console.log(users); // ["Rohan", "Sohan", "Shubham", "Radhe", "Raju", "Abhishek", "Pritam"]

An Array can store any type of data

const array = [function getMyName(){return "Pritam Kumar"}, {address: "India"}, true]

console.log(array[0]()) // Pritam Kumar

console.log(array[1]) //{address: "India"}

Some of the useful Array methods

  1. push() method

    const array1 = ['Rohan', 'Sohan', 'Shubham', 'Radhe', 'Raju', 'Abhishek'];
    
    // push method add an element at the end of the array
    array1.push('monu'); //["Rohan", "Sohan", "Shubham", "Radhe", "Raju", "Abhishek", "monu"]
  2. pop() method

    const array2 = ['Rohan', 'Sohan', 'Shubham', 'Radhe', 'Raju', 'Abhishek'];
    
    // pop method removes an element from the end of the array
    array2.pop(); // ["Rohan", "Sohan", "Shubham", "Radhe", "Raju"]
    1. shift() method

    const array3 = ['Rohan', 'Sohan', 'Shubham', 'Radhe', 'Raju', 'Abhishek'];
    
    // shift method will remove the first element of the array
    array3.shift(); // ["Sohan", "Shubham", "Radhe", "Raju", "Abhishek"]
    1. unshift() method

    const array4 = ['Rohan', 'Sohan', 'Shubham', 'Radhe', 'Raju', 'Abhishek'];
    
    //  unshift method adds an element at the beginning of the array.
    array4.unshift('Sandeep'); //["Sandeep", "Rohan", "Sohan", "Shubham", "Radhe", "Raju", "Abhishek"]

Last updated