JavaScript Arrays
What is an Array?
Arrays are used to store multiple values in a single variable.
Array hold many values under a single name, and you can access the values by referring to an index number.
[0] is the first element in an array. [1] is the second. Array indexes start with 0.
Syntax:
var array-name = [item1, item2, ...];
Array Properties
length Property
The length property of an array returns the length of an array (the number of array elements).
Adding Array Elements
The easiest way to add a new element to an array is using the push method.
New element can also be added to an array using the length property:
Loop Array
An array, is using a "for" loop:
Associative Arrays
Many programming languages support arrays with named indexes.
JavaScript does not support arrays with named indexes.
In JavaScript, arrays always use numbered indexes.
When to Use Arrays and Objects.
- JavaScript does not support associative arrays.
- You should use objects when you want the element names to be strings (text).
- You should use arrays when you want the element names to be numbers.
- In JavaScript, arrays use numbered indexes and objects use named indexes
Comments
Post a Comment