Learn Javascript Array Code Syntax with Example

For each programming language array is a most common & popular object. Without array most of the times we can not make our logic. That’s why in this post i will try to give you a complete article on Javascript arrays, syntax with appropriate code examples. We knew that variables are used to store a single piece of information in the memory. Where as array can hold a series of information as its elements. So that you can retrieve value by providing a single name with an element number. Think you have to take 15 numeric inputs from user then apply some logic & generate the output. So it will be cumbersome to think variable names for each of 15 inputs. Such type of problem you can overcome by using array. So we can write the definition of an array is “An array is also a variable of a sequence, or a list of values. A value in a JavaScript array can hold a number or string, function or an object. JavaScript arrays can store any datatype. A single value in a JavaScript array will be treated an element.”

Syntax of an Javascript Array:
Every array has to be declared before using it. In Javascript array declaration syntax is geven below:

var thearrayname=new Array(arraylength)

In the above syntax thearrayname is a variable like array. We can create an array only using the syntax “new Array()”. It’s case sensitive and so “Array” should not be defined as “array”. You can declare the size of the array in arraylength. So, we can store number of variables in the array that we have declared as arraylength. You can declare any size based on your requirement.

Declaring & Assigning Values:
So based on above syntax now we want to declare an array of size 4. Which will hold our favourite colors name. So the Javascript code will be:

var favColor=new Array(4);


Note: Look at the assignment here, you found that i have started from 0. Because Arrays always start from 0th index or position. If we have set the size as 4, the array will have 4 position of 0,1,2 & 3.

You can also declare in the following way:

var favColor=new Array("White","Red","Green","Yellow");

Note: Such arrays are known as “Dense Arrays“. They are declared by listing the values of the array elements in the declaration, in place of the array length.

Few more Information:
1. You can define any element values in any order.
2. You can increase the array length any time.
3. IF you want to assign a value in out range of array index, the value will be discarded.

Retrieving Values:
We have learned how we can stroe values in an array. Now we will learn how we can read values from array. There were two ways we can read values. One is directly calling by index number & another one is looping through array.

Now if you want to print your 4th favorite color name then the code should be:

document.write("My favourite color is : "+favColor[3]);

Now if we want to loop through the array & print all values then the code should be:

<script type="text/javascript">
var favColor=new Array(4);


//To get the length of an javascript array, use its length property
var len=favColor.length;
        document.write("My favourite color "+eval(i+1)+" is : "+favColor[i]+"<br/>");


Download Code Example Javascript Array

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code