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:
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”:
We will print the elements of the created array on the console using the “console.log()” method:
Then, we will add an element “Tulip” in the array “flowers” using the push() method:
Finally, we will print the updated array “flowers” after adding an element in it:
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:
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:
Finally, we will print the array “flowers” on the console:
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:
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:
Print the appended array on the console:
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.