Adding an element in an array using Javascript

Adding an element to an array can be done using different functions for different positions.

Adding an element at the end of the array

This can be accomplished using the push method. For example,

let veggies = ["Onion", "Raddish"];
veggies.push("Cabbage");
console.log(veggies);

This will give the output ?

["Onion", "Raddish", "Cabbage"]

You can also use this to push multiple items at the same time as it supports a variable number of arguments. For example,

let veggies = ["Onion", "Raddish"];
veggies.push("Cabbage", "Carrot", "Broccoli");
console.log(veggies);

This will give the output ?

["Onion", "Raddish", "Cabbage", "Carrot", "Broccoli"]

Adding an element at the start of the array

This can be accomplished using the unshift method. For example,

let veggies = ["Onion", "Raddish"];
veggies.unshift("Cabbage");
console.log(veggies);

This will give the output ?

["Cabbage", "Onion", "Raddish"]

You can also use this to unshift multiple items at the same time as it supports a variable number of arguments. For example,

let veggies = ["Onion", "Raddish"];
veggies.unshift("Cabbage", "Carrot", "Broccoli");
console.log(veggies);

This will give the output ?

["Cabbage", "Carrot", "Broccoli", "Onion", "Raddish"]

Adding an element at a specific position using splice()

The splice() method is the most versatile way to insert elements at any position in an array.

let months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);

This will give the output:

['Jan', 'Feb', 'March', 'April', 'June']

The first argument is the index where insertion starts, the second argument is the number of elements to remove (0 for insertion), and the remaining arguments are the values to insert.

Adding multiple elements with splice()

let fruits = ['Apple', 'Orange'];
fruits.splice(1, 0, 'Banana', 'Mango', 'Grape');
console.log(fruits);
['Apple', 'Banana', 'Mango', 'Grape', 'Orange']

Custom insert function using prototype

You can create a custom insert function for more intuitive positioning:

Array.prototype.insert = function(data, position) {
    if (position >= this.length) {
        this.push(data);
        // Put at the end if position is more than total length of array
    } else if (position <= 0) {
        this.unshift(data);
        // Put at the start if position is less than or equal to 0
    } else {
        this.splice(position, 0, data);
    }
}

let arr = [1, 2, 3, 4];
arr.insert(-1, 2);
console.log(arr);

This will give the output ?

[1, 2, -1, 3, 4]

Comparison of Methods

Method Position Returns Modifies Original
push() End New length Yes
unshift() Beginning New length Yes
splice() Any position Removed elements Yes

Conclusion

Use push() for adding to the end, unshift() for the beginning, and splice() for any specific position. The splice() method is the most flexible option for inserting elements at any index in an array.

Updated on: 2026-03-15T23:18:59+05:30

291 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements