JavaScript

How to Append Value to an Array JavaScript

In JavaScript, there are multiple methods to add an element to an array. You can add a single element, multiple elements, or even an entire array to another. JavaScript also permits you to traverse or modify the arrays using different methods. The Array.prototype object is the source of all properties and methods.

This article will explain the procedures to append value to an array.

How to Append Value to an Array in JavaScript?

For appending a value to an array, you can use the predefined methods of the JavaScript that are given below:

  • push() method
  • unshift() method
  • splice() method

Let’s understand these methods thoroughly.

Method 1: Append Value to an Array Using push() Method

For appending a value to an array, the “push()” is the most commonly used method. It simply pushes the element into the array, or we can say that it appends elements at the end of the array.

Syntax
Follow the given syntax:

Array.push("element");

Here, the “element” is going to be pushed in the “Array” with the help of the “push()” method.

Example
First, we will create an array named “flowers”:

let flowers = ["Rose", "Lily", "Jasmine"];

We will print the elements of the created array on the console using the “console.log()” method:

console.log(flowers);

Then, we will add an element “Tulip” in the array “flowers” using the push() method:

flowers.push("Tulip");

Finally, we will print the updated array “flowers” after adding an element in it:

console.log(flowers);

As you can see in the output that the element “Tulip” is successfully added to our array:

If you want to add an element at the start of the array, check out the next section.

Method 2: Append Value to an Array Using unshift() Method

You can also use the “unshift()” method for appending a value to an array. It appends the value at the start of an array.

Syntax
Use the given syntax for appending a value at the start position of an array using unshift() method:

Array.unshift("element");

Example
We will consider the same array that was created in the previous example named “flowers”. Now, we will add the element “Tulip” at the start of the array by utilizing the “unshift()” method:

flowers.unshift("Tulip");

Finally, we will print the array “flowers” on the console:

console.log(flowers);

The output indicates that the element “Tulip” is successfully added at the start of the array:

Let’s see the next method to add the element in the middle of the array.

Method 3: Append Value to an Array Using splice() Method

Another method is used to append an element in an array called the “splice()” method. It adds the element in the array’s middle where you want. It takes three parameters as an argument.

Syntax
Follow the below-provided syntax for the splice() method:

Arrays.splice(index, deleteCount, "element");

Here, the “index” is the position where a new element should be added, “deleteCount” tells how many elements should be removed, and the “element” is a value that will be added in the array.

Example
Now, we will add the element “Tulip” at the 1st index of an array. In the following code, “1” is the index of an array where the element will be placed, and the “0” indicates that no elements need to be removed from an array:

flowers.splice(1, 0, "Tulip");

Print the appended array on the console:

console.log(flowers);

You can see in the output that the element “Tulip” is successfully placed at the 1st index of an array:

We gathered all the methods to append a value to an array in JavaScript.

Conclusion

For appending a value to an array, you can use the predefined methods of JavaScript, including the push() method, unshift() method, and splice() method. The unshift() method can be used to insert a new element at the beginning of an array, the push() method can be used to insert a new element at the end, and the splice() method can be used to insert a new element in the middle of the array. This article explained the procedures to append values to an array.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.