This article mainly introduces the method of inserting elements in front of the JavaScript array, which involves the usage skills of the unshift method in javascript and is very useful, for more information about how to insert elements before a JavaScript array, see the example in this article. Share it with you for your reference. The details are as follows:
The JS array has an unshift method. you can add several elements before the array. The following is a detailed code demonstration.
The code is as follows:
Click the button to add elements to the array.
Try it
Script
Function myFunction ()
{
Var fruits = ["Banana", "Orange", "Apple", "Mango"];
Fruits. unshift ("Lemon", "Pineapple ");
Var x = document. getElementById ("demo ");
X. innerHTML = fruits;
}
Script
Note:The unshift () method does not work properly in Internet Explorer 8 and earlier, the values will be inserted, but the return value will beUndefined.
After Execution, the array will look like the following:
Lemon, Pineapple, Banana, Orange, Apple, Mango
Unshift has some problems in ie8 and earlier versions. the array inserts elements, but the returned value is undefined.
I hope this article will help you design javascript programs.