Arrays
Arrays store a collection of data, ordered according to their index. The index is also called the index of the array. It starts at 0 and scales up to the total number of elements in the array, minus 1 (0 to n-1).
Let’s learn how to create an array first.
Creating an array
An array corresponds in JavaScript to an Array
class object. We therefore create an array using the new Array
instruction.
However, since arrays are widely used in JavaScript programs, it is also possible to create them using a bracket notation [ and ]
. This is an easier way to use them without going through the Array
class.
Let’s take a detailed look at these two ways to create an array (with brackets and with the Array
class).
Creating an array using square brackets [ and ]
The easiest and fastest way to create an array is to use the bracket notation:
Creating an array using square brackets
var tab = ["Element 1", "Element 2", "Element 3"...