Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript quick array declaration

Is there a Javascript equivalen of Perl's qw() method to quickly create arrays ? i.e.

in Perl @myarray = qw / one two three /;
in Javascript var myarray = ('one', 'two', 'three' );  // any alternative??
like image 551
Lydon Ch Avatar asked Dec 28 '22 20:12

Lydon Ch


1 Answers

To ‘quickly’ write an array, you can do this:

var x = 'foo bar baz'.split(' ');

Especially for large arrays, this is slightly easier to type than:

var x = ['foo', 'bar', 'baz'];

Although obviously, using .split() is much less performant than just writing out the entire array.

like image 161
Mathias Bynens Avatar answered Jan 04 '23 23:01

Mathias Bynens