The string in JavaScript can be converted into a character array by using the split() and Array.from() functions.
Using String split() Function: The str.split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument.
Syntax:
str.split(separator, limit)
Example:
<!DOCTYPE html> <html> <head> <title> String to a character array </title> </head> <body style="text-align:center;"> <h1>GeeksforGeeks</h1> <p id = "one" >GeeksforGeeks: A computer science portal</p> <input type="button" value="Click Here!" onclick="myGeeks()"> <script> function myGeeks() { var str = document.getElementById("one").innerHTML; document.getElementById("one").innerHTML = str.split(""); } </script> </body> </html> |
Output:
Before Clicking the button:

After Clicking the button:

Using Array.from() Function: The Array.from() function is an inbuilt function in JavaScript which creates a new array instance from a given array. In case of a string, every alphabet of the string is converted to an element of the new array instance and in case of integer values, new array instance simple take the elements of the given array.
Syntax:
Array.from(str)
Example:
<!DOCTYPE html> <html> <head> <title> String to a character array </title> </head> <body style="text-align:center;"> <h1>GeeksforGeeks</h1> <p id = "one" >GeeksforGeeks: A computer science portal</p> <input type="button" value="Click Here!" onclick="myGeeks()"> <script> function myGeeks() { var str = document.getElementById("one").innerHTML; document.getElementById("one").innerHTML = Array.from(str); } </script> </body> </html> |
Output:
Before Clicking the button:

After Clicking the button:

Recommended Posts:
- How to get the last character of a string in JavaScript?
- How to get the longest string in an array using JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- Delete first character of a string in JavaScript
- How to remove portion of a string after certain character in JavaScript ?
- How to remove a character from string in JavaScript ?
- How to get nth occurrence of a string in JavaScript ?
- JavaScript method to get the URL without query string
- How to get the entire HTML document as a string in JavaScript ?
- How to pad a string to get the determined length using JavaScript ?
- How to use Regex to get the string between curly braces using JavaScript ?
- How to get value of a string after last slash in JavaScript?
- Get the first and last item in an array using JavaScript
- How to get all unique values (remove duplicates) in a JavaScript array?
- JavaScript | Get all non-unique values from an array
- How to get a list of associative array keys in JavaScript ?
- How to get array structure with alert() in JavaScript?
- How to get values from html input array using JavaScript ?
- Difference between String and Character array in Java
- Convert a String to Character array in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


