| JavaScript - Arrays | | Print | |
Arrays
An array is a type of object that contains numbered values rather than named values. The [ ] operator is used to access the numbered values of an array:
a[0] = 1;
a[1] = a[0] + a[0];
The first element of a JavaScript array is element 0. Every array has a length property that specifies the number of elements in the array. The last element of an array is element length-1. Array elements can hold any type of value, including objects and other arrays, and the elements of an array need not all contain values of the same type.
You create an array with the Array( ) constructor:
var a = new Array( ); // Empty array
var b = new Array(10); // 10 elements
var c = new Array(1,2,3); // Elements 1,2,3
As of JavaScript 1.2, you can use array literal syntax to include arrays directly in a program. An array literal is a comma-separated list of values enclosed within square brackets. For example:
var a = [1,2,3];
var b = [1, true, [1,2], {x:1, y:2}, "Hello"];
See Array in the reference section for a number of useful array manipulation methods.
| Users' Comments (0) |
|
No comment posted





