Javascript Array


Array storing a collection of multiple values under a single variable. In javascript array is none primitive. There are a lot of built-in methods of an Array in javascript. For example, forEach, map, filter, indexOf, and so on.


Syntax:

1
var age = [10, 20, 30, 40, 50, 60];

OR

1
const cars = new Array("BMW", "Honda Civic", "Mehran");

We can initialize an empty array and then assign values to each index.

For example,

1
2
3
const cars = [];
cars[0] = "BMW";
cars[1] = "Honda Civic";

We can access each value of the array with an index. For example, myArray[0] will give the first value of the given array. Remember array index always starts from 0.


Share