JavaScript Program to Add Element to Start of an Array

Updated on September 30, 2024
Add Element to Start of an Array header image

Introduction

JavaScript arrays are flexible data structures that allow easy manipulation of their contents. One common task when working with arrays is adding new elements to them. Adding elements at the beginning of an array can be particularly useful in certain programming scenarios, such as maintaining a history of values where the most recent entry must be quickly accessible.

In this article, you will learn how to use various methods to add elements to the beginning of an array in JavaScript. Discover how to utilize built-in JavaScript methods effectively through practical examples.

Add Element with unshift()

Using the unshift() Method

  1. Understand that the unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

  2. Declare an array and use unshift() to add a new element at the start.

    javascript
    let fruits = ['Apple', 'Banana'];
    let newLength = fruits.unshift('Strawberry');
    console.log(fruits);
    console.log("New array length: ", newLength);
    

    Here, the unshift() method adds 'Strawberry' to the beginning of the fruits array. It then returns the new length of the array, which is 3 in this case.

Adding Multiple Elements

  1. Use unshift() to add multiple elements at a time to the array's start.

    javascript
    let numbers = [3, 4, 5];
    numbers.unshift(1, 2);
    console.log(numbers);
    

    The example demonstrates adding both '1' and '2' to the beginning of the numbers array. The array changes from [3, 4, 5] to [1, 2, 3, 4, 5] immediately.

Other Techniques to Add Elements

Using the Spread Operator

  1. Understand that the spread operator allows you to expand elements of an iterable (like an array) into individual elements.

  2. Use the spread operator to prepend elements to an array by combining it with array literal syntax.

    javascript
    let originalArray = ['a', 'b', 'c'];
    let newArray = ['start', ...originalArray];
    console.log(newArray);
    

    In this snippet, 'start' is added to the beginning of a new array which then includes all elements of originalArray. The resultant array is ['start', 'a', 'b', 'c'].

Using Array.prototype.concat()

  1. Recall that the concat() method is used to merge two or more arrays, but it does not change the original arrays.

  2. Utilize concat() to prepend an element effectively.

    javascript
    let a = [1, 2, 3];
    let b = [0].concat(a);
    console.log(b);
    

    Here, a single-element array [0] is concatenated with a, resulting in a new array [0, 1, 2, 3] without altering the original a array.

Conclusion

Adding elements to the start of an array in JavaScript is straightforward and can be accomplished through various methods such as unshift(), the spread operator, and concat(). Understanding and applying these methods enhances the versatility of array handling in your JavaScript projects. Depending on the specific requirements, such as performance considerations or immutability, choose the most suitable approach to maintain efficient and readable code.