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?

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']
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

Comments

Your Answer

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.