How to get character array from string in JavaScript?
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 remove a character from string in JavaScript ?
- Delete first character of a string in JavaScript
- How to remove portion of a string after certain character in JavaScript ?
- Convert a String to Character array in Java
- Difference between String and Character array in Java
- JavaScript | length of string and array objects
- Convert comma separated string to array using JavaScript
- Sort array of objects by string property value in JavaScript
- How to find out which character key is pressed using JavaScript?
- How to replace lowercase letters with an other character in JavaScript ?
- How to remove the first character of string in PHP?
- How to remove portion of a string after a certain character in PHP?
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Difference between String.slice and String.substring
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.



