JavaScript new Array

Source: Internet
Author: User
Tags arrays

Javascrip new Array

Create an array

There are several different ways to create an array. Creates an array of arrays to participate in () construct the old way.

JavaScript arrays are dynamic, and you can declare an array without constructing the array parameters ().

In this case, you will create an empty array with no elements.

Select Actionselect alltry it<script type= "Text/javascript Tutorial" >

We initialize the array using the array () constructor.
var first_array = new Array ();

First_array[0] = "This is a element";
FIRST_ARRAY[1] = 5;
FIRST_ARRAY[2] = "javascript-tutorial";
FIRST_ARRAY[3] = 16;
FIRST_ARRAY[4] = 7;

var counter=0;

Let's print out the elements of the array.
for (counter=0; counter<first_array.length; counter++)
document.write (First_array[counter] + "<br>");

</script>


Select Actionselect alltry it<script type= "Text/javascript" >

We declare the "a" single integer as a argument.
var cars = new Array (5);

Cars[0] = "Audi";
CARS[1] = "Bentley";
CARS[2] = "Mercedes";
CARS[3] = "Mini";
CARS[4] = "BMW";

Now we are declare the second array and pass 8 arguments.
This technique does isn't work in JavaScript 1.2.
var flowers = new Array ("Rose", 2.45, "Daisy", 1.57, "Orchild", 0.75,

"Tulip", 1.15);

var counter=0;
document.write ("

for (counter=0; counter<cars.length; counter++)
document.write (Cars[counter] + "<br>");

counter=0;
document.write ("

For (counter=0 counter<flowers.length; counter++) {

if (counter% 2 = 0) {
document.write (Flowers[counter] + "costs");
}
else {
document.write (Flowers[counter] + "<br>");
}

}

</script>

Related Article

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.