user19206
user19206

Reputation: 3

Javascript- How to return an array by with a string as parameter?

For a simple function, how can I return an array with a string as a parameter? I want the array to split the string at the spaces so it returns as [" ", " "].

I tried using the split method split(" ", 2) but it didn't seem to work.

Upvotes: 0

Views: 56

Answers (1)

Shryme
Shryme

Reputation: 1570

var arr = str.split(" ");

You don't need to add 2 after.

The 2 limit the number of value in the array, if you want to split the string without a max, then take out the 2.

The split will "split" the string with " ", if you want to split with other character, replace the space by what you need.

Upvotes: 2

Related Questions