JavaScript Arrays

JavaScript Arrays – Arrays are used to store multiple elements in a single variable. Elements in an array are assumed to represent members belonging to a type. For example, prime numbers, names of students in a class, items to be displayed under a category, etc. Array object in JavaScript helps in the construction of arrays. It also provides methods that help in accessing or transforming or applying functions on the elements of array.

Syntax

var arrayName = [ element1, element2, .. elementN ]

arrayName : is the name given to the array.

During initialization, N number of Elements could be grouped as an array, separated by comma (,).

To access an element of an array, you may use index [0:N-1] with arrayName as shown below :

arrayName[i]

accesses (i+1)th element of the array.

JavaScript – Number Array

To initialize a JavaScript Array Object with Numbers, and access its elements :

[su_button url=”https://www.tutorialkart.com/online/javascript-editor.php?file=array_number” target=”blank” style=”flat” background=”#0ba519″ size=”5″ radius=”0″]Try Online[/su_button]

JavaScript – String Array

To initialize a JavaScript Array Object with Strings, and access its elements :

[su_button url=”https://www.tutorialkart.com/online/javascript-editor.php?file=array_string” target=”blank” style=”flat” background=”#0ba519″ size=”5″ radius=”0″]Try Online[/su_button]

JavaScript – Boolean Array

To initialize a JavaScript Array Object with Numbers, and access its elements :

[su_button url=”https://www.tutorialkart.com/online/javascript-editor.php?file=array_boolean” target=”blank” style=”flat” background=”#0ba519″ size=”5″ radius=”0″]Try Online[/su_button]

JavaScript – Object Array

To initialize a JavaScript Array Object with Numbers, and access its elements :

[su_button url=”https://www.tutorialkart.com/online/javascript-editor.php?file=array_object” target=”blank” style=”flat” background=”#0ba519″ size=”5″ radius=”0″]Try Online[/su_button]

JavaScript – Heterogeneous Array

Heterogeneous Array is an array that contain elements of different primitive types and ofcourse other objects. A JavaScript Array object can contain another array as an element, along with numbers, strings or booleans.

To initialize a JavaScript Array Object with Heterogeneous types of elements, and access its elements :

[su_button url=”https://www.tutorialkart.com/online/javascript-editor.php?file=array_hetero” target=”blank” style=”flat” background=”#0ba519″ size=”5″ radius=”0″]Try Online[/su_button]

Conclusion :

In this JavaScript Tutorial, we have learnt about JavaScript Arrays, how they are initialized etc. In our next tutorial – JavaScript – Array Methods, we shall learn about different methods that an Array have.