JavaScript Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Java-Script Tutorial
Introduction Introduction
Variables Variables
Operators Operators
Statements Statements
Loops Loops
Functions Functions
Objects Objects
Arrays Arrays
Cookies JS Cookies
Browser Objects Browser Objects
Form DOM Form DOM
Style Objects DOM Style Objects
Forums Ask Your Doubts
Scraps More about Javascript
Feedback Feedback
 




Dynamic Array


Tutorials Javascript

Topic

How to create an array with out specifying any length?
or
What are dense arrays and how can we use them?



Explanation

Dynamic Arrays:
       In many a cases we will not want to create the array with a fixed size or length. In such cases we can create an array with out passing length. This array will dynamically set its value as and when a new variable or entry is added.

Syntax:
       var araay = new Array();

Now we can assign value at any position in this array as it has no length limit. We can test this by using the attribute "length".
e.g:
       varname[6] = "testing array";

As we have assigned a value at 6th position the length of array will now be 7.
Example Code:
<script language="javascript">
var araay = new Array();
araay[6] = "testing array";
document.write("Size of dynamic array is - "+araay.length);
</script>


Result:


Dense Array:
        Dense array is nothing different in functionality from a normal array. The only difference is that the values are assigned to the array at the time of initialization of the array.
E.g: var arraa = new Array("index 0","index 1","index 2");

Example Code:
<script language="javascript">
var arraa = new Array("array test 1","array test 2");
document.write("Result from dense array is - "+arraa[1]);
</script>


Result:

In the next chapter we will look into the predefined methods of Array object ..





A Note
Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script. Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.

Other Links

web hosting