DEV Community

Cover image for Intro to Array in JavaScript.
Keshav Jindal
Keshav Jindal

Posted on

Intro to Array in JavaScript.

1. Introduction
Array is a unique kind of variable which can store multiple values. It is enclosed in square [] brackets.

Syntax

const variable_name= [//"data"];
Enter fullscreen mode Exit fullscreen mode

2. Its importance
Array is very helpful as it makes our work easier and also make code less complicated and simple. We don't need to create multiple variables as we can store multiple kind of data in one.
NOTE
We can modify the data in array once declared.
3. Array length
We can access the no of items in the array using 'length' keyword.
For example:

const cars= ['BMW', 'Audi', 'Mercedes'];
console.log(cars.length)
Enter fullscreen mode Exit fullscreen mode

Output for above example is:
3 is output, as there are three items in the array.
Example:

const laptops = ['Lenovo', 'Dell', 'HP'];
console.log(laptops.length)
Enter fullscreen mode Exit fullscreen mode

output:
3
4. Array Push
Array Push adds the item at the end of the sequence.
For example:

const fruits= ['Apple', 'Mango', 'Banana'];
fruits.push('Orange');
console.log(fruits)
Enter fullscreen mode Exit fullscreen mode

Output:
["Apple", "Mango", "Banana", "Orange"]
In above example, an array named fruits was created and in statement 2, using push keyword, 'orange' was added in the list.

Top comments (0)