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.

The syntax to initialize an Array in JavaScript is

</>
Copy
var arrayName = [ element1, element2, .. elementN ]

where arrayName is the name given to the array and would be referenced by this name in the script.

During initialization, the elements are enclosed in opening and closing square brackets, where the elements are separated by comma character.

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

</>
Copy
arrayName[index]

Arrays based on Datatype

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.