Home  >  Article  >  Web Front-end  >  How to insert elements in front of JavaScript array_javascript tips

How to insert elements in front of JavaScript array_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:05:331945browse

The example in this article describes how to insert elements in front of a JavaScript array. Share it with everyone for your reference. The details are as follows:

JS array has an unshift method that can add several elements in front of the array. The following is a detailed code demonstration

Copy code The code is as follows:



Click the button to add elements to the array.



<script><br> function myFunction()<br> {<br> var fruits = ["Banana", "Orange", "Apple", "Mango"];<br> fruits.unshift("Lemon","Pineapple");<br> var x=document.getElementById("demo");<br> x.innerHTML=fruits;<br> }<br> </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 be undefined.



After execution, the array will look like the following

Lemon,Pineapple,Banana,Orange,Apple,Mango

There are some problems with unshift working in IE8 and earlier versions. Elements will be inserted into the array, but the return value is undefined

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn