Add an element to array in JavaScript
The following snippets allows you to add an element to array in JavaScript.
NOTE: Methods with (*) are mutating.
Method 1: Using push()
*
let myArray = [1, 2, 3, 4] myArray.push(5) console.log(myArray) // [1, 2, 3, 4, 5]
Adds an element to the end of the array and mutates itself.
Method 2: Using concat()
let myArray = [1, 2, 3, 4] myArray = myArray.concat(5) console.log(myArray) // [1, 2, 3, 4, 5]
Adds an element to the end of the array and returns a new array.
Method 3: Using length
let myArray = [1, 2, 3, 4] myArray[myArray.length] = 5 console.log(myArray) // [1, 2, 3, 4, 5]
Adds an element to the end of the array using the next index of the last element.
Method 4: Using unshift()
*
let myArray = [1, 2, 3, 4] myArray.unshift(5) console.log(myArray) // [5, 1, 2, 3, 4]
Adds an element to the start of the array and mutates itself.
Method 5: Using splice()
*
let myArray = [1, 2, 3, 4] myArray.splice(myArray.length, 0, 5) console.log(myArray) // [1, 2, 3, 4, 5]
Adds an element at a specific index. In this example, we used the length of the array as the index to add an element at the end of the array.
let myArray = [1, 2, 3, 4] myArray.splice(0, 0, 5) console.log(myArray) // [5, 1, 2, 3, 4]
Adds an element at a specific index. In this example, we used 0 as the index to add an element at the start of the array.
let myArray = [1, 2, 3, 4] myArray.splice(1, 0, 5) console.log(myArray) // [1, 5, 2, 3, 4]
Adds an element at a specific index. In this example, we used 1 as the index.
Method 6: Using the spread operator
let myArray = [1, 2, 3, 4] myArray = [...myArray, 5] console.log(myArray) // [1, 2, 3, 4, 5]
Adds an element to the end of the array and returns a new array.
let myArray = [1, 2, 3, 4] myArray = [5, ...myArray] console.log(myArray) // [5, 1, 2, 3, 4]
Adds an element to the start of the array and returns a new array.
Do you have any more ways to add an element to array in JavaScript? Share it on the comments below.