views:

48

answers:

3

In PHP I can do like:

$arrayname[] = 'value';

Then the value will be put in the last numerical key eg. 4 if the 3 keys already exists.

In JavaScript I can’t do the same with:

arrayname[] = 'value';

How do you do it in JavaScript?

+6  A: 

You can use array push method

arrayname.push('value');
Chandra Patni
+3  A: 

You can use the push method.


For instance (using Firebug to test quickly) :

First, declare an array that contains a couple of items :

>>> var a = [10, 20, 30, 'glop'];

The array contains :

>>> a
[10, 20, 30, "glop"]


And now, push a new value to its end :

>>> a.push('test');
5

The array now contains :

>>> a
[10, 20, 30, "glop", "test"]
Pascal MARTIN
+4  A: 

You can use

arrayName.push('yourValue');

OR

arrayName[arrayName.length] = 'yourvalue';

Thanks

Mahesh Velaga