Introduction to Arrays in JavaScript

JavaScript arrays are used to store multiple values in a single variable. An array in JavaScript can hold different elements We can store Numbers, Strings and Boolean in a single array. Also arrays in JavaScript are dynamic in nature, which means its size can increase or decrease at run time.

Syntax –

Use the following syntax to create an Array object −

Access the Elements of an Array –

You access an array element by referring to the index number. This statement accesses the value of the first element in cars:

Access the Full Array –

With JavaScript, the full array can be accessed by referring to the array name:

The length Property –

The length property of an array returns the length of an array (the number of array elements).

Looping Array Elements –

The safest way to loop through an array, is using a “for” loop:

 

Leave a Reply

Your email address will not be published. Required fields are marked *