How to add spaces between every character in a string?

You can use the split() function to turn the string into an array of single characters, and then the join() function to turn that back into a string where you specify a joining character (specifying space as the joining character):

function insertSpaces(aString) {
  return aString.split("").join(" ");
}

(Note that the parameter to split() is the character you want to split on so, e.g., you can use split(",") to break up a comma-separated list, but if you pass an empty string it just splits up every character.)

Leave a Comment