Add whitespace to an array in javascript

Simply put, I want to split a string in javascript into an array, which should also include all the whitespaces in the original string.

Ex: "Hi There" should be broken down to ['H','i',' ','T','h','e','r','e'] I tried using str.split('') only to find the results as ['H','i','','T','h','e','r','e'] I want a whitespace after "i" in "Hi" here in this example

Answer

Try this:

const result = "Hi There".split(""),
      solution = [..."Hi There"];
      

console.log(result);
console.log(solution);

Leave a Reply

Your email address will not be published. Required fields are marked *