4

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
1
  • 1
    nope sorry that does not exist at this time. you could do "a b c".split(" "), but that's extra overhead. Commented Jul 23, 2014 at 14:24

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.