JAVASCRIPT

 Home  Computers & Internet  Web Programming JAVASCRIPT
What is Javascript?
Javascript Placement
Syntax
Reserved Words
Variables
Data Types
Escaping Characters
Concatenation
Arithmetic Operators
Assignment Operators
Comparison Operators
Boolean Operators
Conditional Operator
If Statements
Else If Statements
If Else Statements
Switch Statements
While Loops
For Loops
Do While Loops
Break Statement
Continue Statement
prompt()
alert()
Date()
Event Handlers
String Object Methods
Math Object Methods
Window Object Methods
Arrays

HOW TO ADD ELEMENTS TO AN ARRAY IN JAVASCRIPT

Adding one or more elements to an existing javascript array is relatively simple:

To add elements individually without specifiying index numbers, you will need to first find the next available index number. This can be achieved using the length property. The length property gives you the number of elements in an existing array. As an array's index starts at 0, the array length property will give the next available index number as in in this example:

var colors=new Array("blue", "green");
Above equivalent to:
count[0]="blue";
count[1]="green";

alert(colors); // displays blue, green

var Count=colors.length;

var Count gives 2 which are the number of elements in this array colors. Since blue is at index 0 (array indexes always start at 0) and green is at index 1, the next available index number is 2 which is the same as the array length.

Therefore to add an element to this array, we simply do:

colors[colors.length]="red";

alert(colors); // displays blue, green, red

This array is now equivalent to:
count[0]="blue";
count[1]="green";
count[2]="red";

Now Read:

How To Create An Array
How To Get Array Size
How To Loop Through An Array
How To Add Elements To An Array
How To Join Array Elements
How To Reverse Element Order In An Array
How To Slice Elements Of An Array Into Pieces
How To Sequentially Sort Elements In An Array

Home | Privacy Policy | Terms Of Use | Contact Us | Work At Home | Online Jobs | Smart Info | Online Jobs | Data Entry Jobs | Paid Surveys | Info | Info | Typing Jobs | Article Archive | Forum Posting Jobs