How To Insert Element To Front/Beginning Of An Array In JavaScript


In this tutorial, you’ll learn how to insert element to front/beginning of an array in JavaScript.

For adding an element to an array, you use push which inserts the new element to the end of the array.

    let array_list = [1, 2, 3]
    array_list.push(4);
    console.log(array_list);
    
    Output : 1, 2, 3, 4

To insert an element to the front or beginning of an array you can use the unshift method.

    let array_list = [1, 2, 3]
    array_list.unshift(4);
    console.log(array_list);
    
    Output : 4, 1, 2, 3

Conclusion

In this quick tutorial, you learnt how to push and unshift to add elements to the end and front/beginning of an array using JavaScript.