Dynamic Array

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:


<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:


<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 ..

Ask Questions

Ask Question