How to Add Element at the Beginning of an Array in Javascript?
Aug 31, 2022 . Admin
Hello dev,
This is a short guide on how to add elements at the beginning of an array in javascript. you will learn how can i add new array elements at the beginning of an array in javascript. we will help you to give an example of a javascript program to add elements to the start of an array.
In this example, we will explain to you how to add elements at the beginning in javascript. if you add an element at the beginning, you have to use unshift() method. it inserts the given values to the beginning of an array-like object. it takes in an arbitrary number of elements to add to the array.
So, let's see bellow solution:
Example:<!DOCTYPE html> <html> <head> <title>How to Add Element at the Beginning of an Array in Javascript? - MyWebtuts.com</title> </head> <body> <h1>How to Add Element at the Beginning of an Array in Javascript? - MyWebtuts.com</h1> <script type="text/javascript"> const fruits = ["Mango","Banana","Apple","Orange"]; fruits.unshift("Pineapple","Kiwi"); //give output to console.. console.log(fruits); </script> </body> </html>Output:
['Pineapple', 'Kiwi', 'Mango', 'Banana', 'Apple', 'Orange']I hope it will help you...