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:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
Before Clicking the button:

After Clicking the button:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.