This page looks best with JavaScript enabled

How to add an items at the beginning of an array in JavaScript

 ·  โ˜• 1 min read  ·  ๐Ÿ‘ฝ john hashim

This can be done by using splice() method of an array.

splice() takes more than 3 arguments. The first is the start index: the place where weโ€™ll start making the changes. The second is the delete count parameter. Weโ€™re adding to the array, so the delete count is 0 in all our examples. After this, you can add one or many items an array as you like.

1
2
3
4
5
const fruits = ['mango', 'orange']

colors.splice(0, 0, 'apple')

//['apple', 'mango', 'orange']
Share on

john hashim
WRITTEN BY
john hashim
Web Developer