JavaScript – Convert String to Array of Characters

To convert given string into an array of characters in JavaScript, use String.split() method.

split() method takes separator string as argument, and splits the calling string into chunks, and returns them as an array of strings. To split the string into an array of characters, pass empty string "" as argument to split() method.

The syntax of the expression that returns an array of characters for a string str is

</>
Copy
str.split("");

Example

In the following example, we take a string in str, split this string into an array of characters using String.split() method, and display the array of characters in pre#output.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
  <pre id="output"></pre>
  <script>
    var str = 'apple';
    var charArray = str.split("");
    for(let i=0; i < charArray.length; i++) {
      document.getElementById('output').innerHTML += charArray[i] + '\n';
    }
  </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to convert a string into an array of characters in JavaScript using String.split() method, with example program.