Javascript - How to access each character of a string

By xngo on June 17, 2019

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 
    <script type="text/javascript">
      /**
       * Example showing how to access each character of a string using the charAt() function.
       */
 
      var sVeryLongString = "Hello, world!";
 
      var sEachCharacters = "";
      for(i=0; i<sVeryLongString.length; i++)
      {
        sEachCharacters += sVeryLongString.charAt(i)+"\n";
      }
 
      window.alert(sEachCharacters);
 
    </script>
 
    <title>Access each charater of a string</title>
  </head>
 
  <body>
  </body>
</html>

Output

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.