javascript arrays: adding entries
I'm not sure how I w开发者_如何学Pythonould 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
var messages = new Array();
messages.push('Message 1');
messages.push('Message 2');
messages.push('Message n');
that should do the trick :)
reference here
精彩评论