Method _ JavaScript tips for inserting elements before a javascript array

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.