CodewithKamrul

How to add an element at the first position of an array in javascript?

In javascript add one or more elements at the beginning of an array using unshift( ) method.

Example of add one element beginning of an array:

const friends = ["bellal", "salam", "jabbar", "kuddus"]
friends.unshift("Shafiq");
console.log(friends); // ["Shafiq", "bellal", "salam", "jabbar", "kuddus"]
Example of add more than one element at the beginning of an array:
const friends = ["bellal", "salam", "jabbar", "kuddus"]
friends.unshift("Shafiq", "shaka", "ismail");
console.log(friends); // ["Shafiq", "shaka", "ismail", "bellal", "salam", "jabbar", "kuddus"]

Leave a Comment