Inserting Elements into an Array in JavaScript

To insert elements into an array in JavaScript, you can use the array.push(elements), array.unshift(elements), array.splice(start, deleteCount, elements) methods, and the spread ("...") operator. The push() methods allow you to add one or more elements to the end of an array. The unshift() method allows you to add one or more elements to the beginning of an array. The push() and unshift() methods change the original array and its length. The splice() method allows you to change the contents of an array by removing or replacing existing elements or adding new elements to the array. The splice() method modifies an existing array rather than returning a new one. You can also use the spread operator ("..."), which is the modern ES6 way of inserting elements into an array. In this JavaScript Array Insert Element example, we use the push() method to insert an element into the array. Below you can see more examples of inserting elements into a JavaScript array using the push(), unshift(), and splice() methods, with a detailed description of each method. Click Execute to run the JavaScript Array Insert Element Example online and see the result.
Inserting Elements into an Array in JavaScript Execute
let array = ['JavaScript', 'Array', 'Insert'];

array.push('Example');
   
console.log(array);
Updated: Viewed: 6449 times

Insert elements into a JavaScript array using the push() method

The array.push(element1, element2, ...) method in JavaScript is used to add one or more elements to the end of an array. The push() method overwrites the original array and change the array's length by the number of elements added to the array.

JavaScript Array push() Syntax
array.push(element1, element2, ..., elementX)

Where:
  • array: specifies the array where the element will be inserting
  • element1, element2, ..., elementX: one or more comma-separated elements to be inserted to the end of the array. At least one element must be specified
JavaScript Array Insert with push() Example
let array = ['1', '2', '3'];

array.push('4');
   
console.log(array);

// output: ['1', '2', '3', '4']

Insert elements into a JavaScript array using the unshift() method

The array.unshift(element1, element2, ...) method in JavaScript is used to add one or more elements to the front of an array. The unshift() method overwrites the original array and change the array's length by the number of elements added to the array.

JavaScript Array unshift() Syntax
array.unshift(element1, element2, ..., elementX)

Where:
  • array: specifies the array where the element will be inserting
  • element1, element2, ..., elementX: one or more comma-separated elements to be inserted at the array's beginning. At least one element must be specified
JavaScript Array Insert with unshift() Example
let array = ['1', '2', '3'];

array.unshift('0');
   
console.log(array);

// output: ['0', '1', '2', '3']

Insert elements into a JavaScript array using the splice() method

The array.splice(start, deleteCount, element1, ..., elementX) method in JavaScript is used to modify the contents of an array by deleting existing elements and/or adding new elements to the array. The splice() method modifies the original array.

JavaScript Array splice() Syntax
array.splice(start, deleteCount, element1, ..., elementX)

Where:
  • array: specifies the array where the element will be inserting
  • start: an integer that specifies the array index from which elements will be removed from the array and added to the array. Negative values are allowed, in which case the index from which the method will be called will be calculated using the following formula: length + start
  • deleteCount (optional): an integer specifying the number of elements to remove from the array, starting at the index specified in the start. If deleteCount is 0, then the elements are not deleted. If deleteCount is more significant than the number of remaining elements in the array, then all remaining elements of the array will be deleted
  • element1, ..., elementX (optional): one or more elements to be inserted into the array. The array index at which new elements will be inserted corresponds to the start parameter.
JavaScript Array Insert with splice() Example
let array = ['JavaScript', 'Example'];

array.splice(1, 0, "Array", "Insert");

console.log(array);

// output: ['JavaScript', 'Array', 'Insert', 'Example']

Insert elements into a JavaScript array using spread operator

In JavaScript ES6 and above, we can use the spread operator ("..."). The spread operator is a flexible way of working with arrays popular in "new" JavaScript.

JavaScript Array Insert with Spread Operator Example
let arr = ['1', '2', '3'];

let newArray = ['0', ...arr, '4'];

console.log(newArray);

// output: ['0', '1', '2', '3', '4']

See also