This post will discuss how to insert items to the end of an array in JavaScript.

There are several ways to insert an item at the end of an array in JavaScript. Some of the most common functions are:

1. Using push() function

The Array.push() function adds one or more elements to the end of an array and returns the new length of the array. It is simple and effective, and it modifies the original array.

Download  Run Code

2. Using spread syntax

The spread syntax (…) allows us to expand the elements of an iterable (such as an array) into individual arguments. It can be used to create a new array by combining the existing array and the new element. It does not affect the original array, but returns a new array with new items appended.

Download  Run Code

3. Using concat() function

The concat() function creates a new array by concatenating two or more arrays. It does not change the original arrays, but returns a new array with new items appended.

Download  Run Code

4. Using length property

The length property returns or sets the number of elements in an array. It can be used to insert an item at the end of an array by assigning the item to the index equal to the current length of the array.

Download  Run Code

5. Using splice() function

The Array.splice() function can be used to insert an element at a specific position in an array. It can be used to insert an item at the end of an array by specifying the array length as the start index, 0 as the delete count, and the item as the element to add.

Download  Run Code

That’s all about inserting items to the end of an array in JavaScript.