methuselah methuselah - 1 month ago 6
Javascript Question

Split string into array

In JS if you would like to split user entry into an array what is the best way of going about it?

For example:

entry = prompt("Enter your name")

for (i=0; i<entry.length; i++)
entryArray[i] = entry.charAt([i]);

// entryArray=['j', 'e', 'a', 'n', 's', 'y'] after loop

Perhaps I'm going about this the wrong way - would appreciate any help!


Use the .split() method. When specifying an empty string as the separator, the split() method will return an array with one element per character.

entry = prompt("Enter your name")
entryArray = entry.split("");