Jobs
Blogs
Courses
Mentorship
Partners
For Employers
e2e logo

JavaScript Arrays

mac

JavaScript Arrays


Arrays are special variables that can hold multiple values. JavaScript array is an object that represents a collection of similar types of elements. 

Creating an Array :

Using an array literal is the easiest way to create an array.

Syntax

As you can see the above syntax values are contained inside the [ ], and separated by a comma (,). 

An example is given below

Spaces and line breaks are not important, a declaration can span multiple lines.

You can also create an empty array and then you can provide the elements of an array.

and also using the JavaScript keyword new we can create an array.

The above two examples do exactly the same so no need to use the new Array().

For simplicity, and readability use the array literal method.

Access an Array Elements

You can access the array elements by using the index number

Array indexes start with 0, [0] is the first element, and [1] is the second element.
Changing the Array Elements
The below given example changes the element of a given array.