How to declare array in JavaScript

{0 Comments}

You can declare array in JavaScript in three ways. First one using JavaScript array constructor, second is literal notation, third way is calling method with array return type.

Following is the syntax to declare JavaScript array with array constructor here new keyword is to define array in memory

 Javascript |  copy code |? 
1
var abcarray = new Array();
2
           var abcarray1= new Array("first","secound","third");

Lets come to second way to declare a array in JavaScript which is Literal notation in place new key word we [] bracket directly which will the another basic syntax to declare a array in JavaScript. These are as follow:

 Javascript |  copy code |? 
1
var abc_array=[];
2
           var abc_array1=["first","secound","third"];

Now the third way to declare JavaScript array is calling a method with return type of array. See the following code which shows to use the following method. In this code we use a method which use to split the string and it return the value in array form. 

 Javascript |  copy code |? 
1
var str="there is some thing strange";
2
         var n=str.split(" "); 

Leave a Comment

Your email address will not be published.