unshift()
MethodYou can use the unshift()
method to easily add new elements or values at the beginning of an array in JavaScript. This method is a counterpart of the push()
method, which adds the elements at the end of an array. However, both method returns the new length of the array.
The following example will show you how to add single or multiple elements at the start of an array.
<script>
var fruits = ["Apple", "Banana", "Mango"];
// Prepend a single element to fruits array
fruits.unshift("Orange");
console.log(fruits);
// Prints: ["Orange", "Apple", "Banana", "Mango"]
// Prepend multiple elements to fruits array
fruits.unshift("Guava", "Papaya");
console.log(fruits);
// Prints: ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"]
// Loop through fruits array and display all the values
for(var i = 0; i < fruits.length; i++){
document.write("<p>" + fruits[i] + "</p>");
}
</script>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com
Laravel 6 - Multiple Files upload with Validation Example
Today, i will share with you How to done...How to Set CSS background-image Property Using jQuery
Use the jQuery CSS() Method To set th...How to add attributes to an HTML element in jQuery
Use the jQuery attr() method You can...How to Capture Browser Window Resize Event in JavaScript
Use the addEventListener() Met...How to Get the Value of Text Input Field Using JavaScript
Use the value Property You...