Is there a quick way to create a literal array filled with strings in javascript?
I am coming from Ruby, where using %w{} allows for you to omit quotation marks and commas around the values of the array. For example:
array = %w{a b c}
=> ["a", "b", "c"]
is equivalent to the standard syntax for literal assignment:
array = ["a", "b", "c"]
=> ["a", "b", "c"]
Is there anything similar to this in javascript?
asked Jul 23, 2014 at 14:23
jbarrieault
46411 silver badges20 bronze badges
2 Answers 2
There may be a better way, but this would work:
var array = 'abc'.split(''); // ['a', 'b', 'c']
And for words:
var array = 'domo arigato mr. roboto'.split(' ');
// ['domo', 'arigato', 'mr.', 'roboto']
answered Jul 23, 2014 at 14:25
pcrglennon
4574 silver badges9 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
I don't know it's a proper way or not but I go with
'abc'.split(''); //returns array
answered Jul 23, 2014 at 14:26
Mritunjay
25.9k7 gold badges57 silver badges71 bronze badges
Comments
lang-js
"a b c".split(" "), but that's extra overhead.