To convert each character in the string into an array, we can use the spread operator ...
on the string inside an array in JavaScript.
Consider a string called Hello!
like this,
// random string
const str = "Hello!";
Now to convert each character in this string into an array, let's use the spread operator ...
on the string inside an array. It can be done like this,
// random string
const str = "Hello!";
// convert each characters in string to array
const strArr = [...str];
console.log(strArr); // ["H", "e", "l", "l", "o", "!"]
Now if you look at the output of the strArr
, we can see that the string is successfully converted to an array.
See the above code live in JSBin.
That's all 😃!