views:

73

answers:

2

Hi,

I'm not sure how I would do this in Javascript. I want to create an array that would contain any number of error messages from my form.

I read on websites that you define the key for that array when adding new entries.

// Javascript
var messages = new Array();
messages[0] = 'Message 1';

How can I add entries into my array using a similar method I normally do in PHP

// PHP    
$messages = array();
$messages[] = 'Message 1'; // automatically assigned key 0
$messages[] = 'Message 2'; // automatically assigned key 1

Is it possible to emulate that in Javascript? I don't want to have to define the number of entries in my array as it can vary.

Thanks for helping me out on this very basic js question. -Lyon

+3  A: 
var messages = new Array();
messages.push('Message 1');
messages.push('Message 2');
Max Shawabkeh
Beat me to it by 7 seconds. ;) +1
Tomalak
For the definition in ecmascript 3rd edition see http://www.mozilla.org/js/language/E262-3.pdf @ "15.4.4.7 Array.prototype.push"
VolkerK
+2  A: 
messages.push('Message n');

that should do the trick :)

reference here

asymmetric
great thanks for your help! :D
Lyon