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 usingsplice()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