Latest posts

10/recent/ticker-posts

Array and Types of Arrays in Javascript | Arrays in Javascript | Array creation in Javascript

 Array and Types of Arrays in Javascript

What is an Array?

The array is a Javascript data structure, that allows you to store multiple values in a single, organized, and ordered collection.
Simply we can say that array is a collection of similar types of data or a collection of values which has similar data types.

How to create an Array?

In Javascript, to create arrays, we can use square brackets ' [] '. It can contain elements of any data type such as Integers, Strings, Objects, and even other arrays.

Syntax :
var array_name = [ele1, ele2, ele2, ...];

Why use arrays?

The basic reason for using arrays in programming is to store multiple values in a single variable.
Suppose, you have a list that has 5 items, then you can easily able to 
For example, if you have a list of Bikes, then you can store the Names of bikes in a single variable.

Example :
/* List without Array */

var Bike1 = "Duke";
var Bike2 = "KTM";
var Bike3 = "Yamaha";

/* List using Array */

var Bikes = ["KTM", "Duke", "Yamaha"];


This is the primary example of using an array. we can able to do various operations using arrays.

Each element of an array has an index, which is a positive integer that starts at 0 for the first element and increments by 1 for each subsequent element.
Because of that, we can easily access a specific element of an array.

For Example, we have a list of fruits Apples, Bananas, Mangoes, and Coko, and we want to display only the Apple from the list. Then we can use the index position of these elements to display them.

Example :
/* List of Fruits */

var fruit1 = "Apple";
var fruit2 = "Banana";
var fruit3 = "Mango";
var fruit4 = "Coko";

/* Displaying Apple from List */

var fruits = ["Apple", "Banana", "Mango", "Coko"];

document.getElementById("fruit").innerHTML = fruits[0];


Creation of Arrays in Javascript :

In Javascript, there are 3 types of arrays are present.
  1. Array using Instance
  2. Array using Literals
  3. Array using Constructor
1) Array using Instance :
It is one of the way of creating arrays in Javascript. In that, firstly we can creates a array by using 'new' keyword and then adds the elements in those array by using their index numbers.

Syntax:
var array_name = new Array(number of elements);

Example :
var car = new Array(5);
car[0] = "Rolce Royce";
car[1] = "BMW";
car[2] = "Audi";
car[3] = "Toyota";
car[4] = "Tesla";
for(var i in car)
{
    document.write(car[i]+"<br>");
}


2) Array Using Literals : 
It is the simplest way of creating arrays. In that, we can create the arrays by simply using the square brackets ' [] ' and adding the elements in that.

Syntax :
var Array_name = [item1, item2, item3, ...];

Example :
var fruits = ["Apple", "Banana", "Mango", "Coko"];
for(var i in fruits)
{
    document.write(fruits[i]+"<br>");
}


3) Array Using Constructor :
It is also a type of array creation. In that, we can create the arrays by using the 'new' keyword and using Parenthesis brackets ( ) to add the elements in the array.

Syntax :
var Array_name = (item1, item2, item3, ...);

Example :
var names = new Array("Rohan", "Aayan", "Ayushaman", "Kartik");
for(var i in names)
{
    document.write(names[i]+"<br>")
}


So these are the types of Array creation, by using this you can able to create arrays in Javascript.

Suggestion : There is no need to remember all the types of Arrays, all types have the same functionality and methods, we just need to be the master of anyone of them.

Hope this post will help you to learn Javascript more...

Thanks for visiting..!!!




Also Visit


Post a Comment

0 Comments