JavaScript tutorial-array


JavaScript array

Array is an object that can store many values of the same type. This means that numerical array can store many numerical values and string array can store many string values. You can refer to an element of the array by using its index. The index of the array starts from 0. So the first element of the array is at index 0. To declare an array, you must write its name.

Example:
nArray = new Array(10);
sArray= new Array(5);
-The word nArray is the name of the array
-The new keyword is used to create array object
-The value 10 or 5 is the size of the array
-The element of array starts from index 0. This means that the nArray has 10 elements which starts from 0 to 9. The sArray has 5 elements which start from 0 to 4.
 
-Assign values to an array

You can assign values to an array by specifying its index in brackets [], as shown below. You can not specify an index that is not in the range. If you try to violate the rule, you will get an error.


nArray[0] = 1;
nArray[1] = 2;
myArray[2] = 3;
sArray[0]=”Dara”;
sArray[4]=”Vibol”;
 
Note: you can declare an array without specifying it size. Therefore, its size can be flexible.
Example:
myArray=new Array();
myArray[10]=101;
 
Note: you can use length property of the array to access the number of elements of the array:


var no= myArray.length;
You can also sort that array by calling sort() method:
myArray.sor();



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.