This website collects cookies to deliver better user experience
How to convert each character in the string to an array using the Array.from() method in JavaScript?
How to convert each character in the string to an array using the Array.from() method in JavaScript?
To convert each character in a string into an array, we can use the from() method from the global Array object in JavaScript.
Consider a string called Hello! like this,
// random stringconst str ="Hello!";
Now to convert each character in this string into an array, let's use the Array.from() method and
pass the above string as the first argument to the method.
the method returns a new array
It can be done like this,
// random stringconst str ="Hello!";// convert each characters in string to arrayconst strArr =Array.from(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.