Lyon
Lyon

Reputation: 7364

javascript arrays: adding entries

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

Upvotes: 1

Views: 229

Answers (2)

asymmetric
asymmetric

Reputation: 3890

messages.push('Message n');

that should do the trick :)

reference here

Upvotes: 2

Max Shawabkeh
Max Shawabkeh

Reputation: 38603

var messages = new Array();
messages.push('Message 1');
messages.push('Message 2');

Upvotes: 3

Related Questions