Skip to main content

Command Palette

Search for a command to run...

All about Arrays in JavaScript

Updated
4 min read
All about Arrays in JavaScript

Array

Arrays are Objects. The type of operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access their "elements".

  • Example:

    const num= ['1', '2', '3'];


Creating an Array

Using an array literal is the easiest way to create a JavaScript Array.

  • Syntax:

    const array_name = [item1, item2, ...];



How to access element of an array:

We can access the elements of an array using its index(index starts with 0).

  • Example

    let num = ['1', '2', '3', '4'];
    console.log("2nd element in array is: ", num[1]);
    

    Output:

    2nd element in array is: 2



JavaScript Array Methods

Converting Arrays to Strings

The JavaScript method toString() converts an array to a string of (comma separated) array values.

  • Example

    let num = ['1', '2', '3', '4'];
    console.log(num.toString());
    

    Output:

    1,2,3,4


JavaScript Array pop()

The pop() method removes the last element from an array:

  • Example

    let num = ['1', '2', '3', '4'];
    let popped = num.pop()
    console.log("The array after pop last element: ", num);
    console.log("The pop element is: ", popped);
    

    Output:

    The array after pop last element: [ '1', '2','3' ]

    The pop element is: 4


JavaScript Array Push()

The push() method adds a new element to an array (at the end):

  • Example

    let num = ['1', '2', '3', '4'];
    let pushed = num.push("5")
    console.log("The new array is: ", num);
    console.log("The element pushed is: ", pushed);
    

    Output:

    The new array is: [ '1', '2', '3', '4', '5' ]

    The element pushed is: 5


JavaScript Array shift()

The shift() method removes the first array element and "shifts" all other elements to a lower index.

  • Example

    let num = ['1', '2', '3', '4'];
    let shifted = num.shift()
    console.log("The new array is: ", num);
    console.log("The element shifted is: ", shifted);
    

    Output:

    The new array is: [ '2', '3', '4' ]

    The element shifted is: 1


JavaScript Array unshift()

The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:

  • Example

    let num = ['1', '2', '3', '4'];
    let unshifted = num.unshift('0');
    console.log("The new array is: ", num);
    

    Output:

    The new array is: [ '0', '1', '2', '3', '4' ]


Changing Elements

Array elements are accessed using their index number:

Array indexes start with 0:

[0] is the first array element

[1] is the second

[2] is the third ...

  • Example

    let num = ['1', '2', '3', '4'];
    num[3] = 'xyz';
    console.log("The new array is: ", num);
    

    Output:

    The new array is: [ '1', '2', '3', 'xyz' ]


JavaScript Array length

The length property provides an easy way to append (insert element at last) a new element to an array:

  • Example

    let num = ['1', '2', '3', '4'];
    num[num.length] = '5';
    console.log("The new array is: ", num);
    

    Output:

    The new array is: [ '1', '2', '3', '4', '5' ]


Concatenate Arrays

The concat() method creates a new array by merging (concatenating) existing arrays:

  • Example-1

    let num = ['1', '2', '3', '4'];
    let num2 = ['5', '6', '7', '8'];
    let Concatenate = num.concat(num2)
    console.log("The new array is: ", Concatenate);
    

    Output:

    The new array is: [ '1', '2', '3', '4', '5', '6', '7', '8' ]

  • Example-2

    let num = ['1', '2', '3', '4'];
    let num2 = ['5', '6', '7', '8'];
    let num3 = ['9', '10', '11', '12'];
    let Concatenate = num.concat(num2,num3)
    console.log("The new array is: ", Concatenate);
    

    Output:

    The new array is: [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']


JavaScript Array splice()

The splice() method can be used to add new items to an array:

  • Example

    let num = ['1', '2', '3', '4'];
    num.splice(2,0, '2.5');
    console.log("The new array is: ", num);
    

    Output:

    The new array is: [ '1', '2', '2.5', '3', '4' ]


JavaScript Array splice()

The splice() method can be used to add new items to an array:

  • Example

    let num = ['1', '2', '3', '4'];
    num.splice(2,0, '2.5');
    console.log("The new array is: ", num);
    

    Output:

    The new array is: [ '1', '2', '2.5', '3', '4' ]


JavaScript Array slice()

The slice() method slices out a piece of an array into a new array.

  • Example

    let num = ['1', '2', '3', '4'];
    let slicing = num.slice(1);
    console.log("The new array is: ", slicing);
    

    Output:

    The new array is: [ '2', '3', '4' ]



JavaScript Sorting Arrays

Sorting an Array

The sort() method sorts an array alphabetically:

  • Example

    let num = ['c', 'b', 'a', 'd'];
    let sorting = num.sort();
    console.log("The sorted array is: ", sorting);
    

    Output:

    The sorted array is: [ 'a', 'b', 'c', 'd' ]


Reversing an Array

The reverse() method reverses the elements in an array.

  • Example

    let num = ['c', 'b', 'a', 'd'];
    let sorting = num.sort();
    let reversing = num.reverse();
    console.log("The sorted array is: ", reversing);
    

    Output:

    The sorted array is: [ 'd', 'c', 'b', 'a' ]

All about Arrays in JavaScript