Basic JavaScript tutorial (3): array, simple javascript

Source: Internet
Author: User

Basic JavaScript tutorial (3): array, simple javascript

This article requires programming experience in other languages.

In JavaScript, arrays are objects (rather than non-linear memory ).

Use the array literal to create an array:

Copy codeThe Code is as follows:
Var empty = [];
Var numbers = [
'Zero ', 'one', 'two', 'three', 'four ',
'Five', 'six', 'seven', 'Eight ', 'nine'
];
Empty [1] // undefined
Numbers [1] // 'one'
Empty. length // 0
Numbers. length // 10

The array has an attribute length (but the object does not), which indicates the length of the array. The value of length is the maximum integer attribute name of the array plus 1:

Copy codeThe Code is as follows:
Var myArray = [];
MyArray. length; // 0
MyArray [1, 1000000] = true;
MyArray. length; // 1000001

We can directly modify the length:

Changing the length value will not lead to more space allocation.
Length is reduced, and all attributes with subscript greater than or equal to length are deleted.
Because the array is also an object, you can use delete to delete the elements in the array:

Copy codeThe Code is as follows:
Delete number [2];
Number [2] = undefined;

In this way, the elements in the array will be deleted with a hole.

JavaScript provides a set of Array methods, which are put in Array. prototype (this is not detailed here ).


Javascript array output problems

Assume that
Input a and B in the first loop.
B = ['A', 'B'];
Input c, d in the second loop.
B = ['C', 'D'];

A's content is
A = [
['A', 'B'],
['C', 'D']
];

Simply put, it is a set of numbers in the array.

Alert (a) should display a, B, c, d.
But here you show c, d, c, d

We recommend that you use the Firebug console for debugging. The debugging results are easy to understand.
If you use the console for debugging, you can replace alert
Console. log (j, B );
Console. log ();

Supplement:
Alert (a) should display a, B, c, d.
But here you show c, d, c, d
The reason is that the content assigned to the variable is array or object, but the address points to array or object, rather than completely copying, so B changes a [I] as well.

Change
A [I] = B. concat ();

It will not change, because this is copying an array.

Create an array that contains three elements and then access the array elements.

Define an array
An array object is used to store a series of values in a separate variable name.
We use the keyword new to create an array object. The following code defines an array object named myArray:
Var myArray = new Array ()
There are two ways to assign values to an array (you can add any number of values, just as you can define any number of variables you need ).
1:
Var mycars = new Array ()
Mycars [0] = "Saab"
Mycars [1] = "Volvo"
Mycars [2] = "BMW"
You can also use an integer independent variable to control the array capacity:
Var mycars = new Array (3)
Mycars [0] = "Saab"
Mycars [1] = "Volvo"
Mycars [2] = "BMW"
2:
Var mycars = new Array ("Saab", "Volvo", "BMW ")
Note: If you need to specify a value or logical value in the array, the variable type should be a numeric or Boolean variable, rather than a character variable.
Access Array
By specifying the array name and index number, you can access a specific element.
The following is the code line:
Document. write (mycars [0])
The output is as follows:
Saab
Modify values in an existing array
To modify the values in an existing array, you only need to add a new value to the specified label:
Mycars [0] = "Opel ";
Now, the above Code is as follows:
Document. write (mycars [0]);
Output:
Opel

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.