Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Get a String to a Character Array in JavaScript</title> </head> <body> <script> // Sample string var str = "Hello World!"; // Splitting the string var chars = str.split(""); console.log(chars); // Prints: ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", "!"] // Accessing individual character document.write(chars[0] + "<br>"); // Outputs: H document.write(chars[1] + "<br>"); // Outputs: e document.write(chars[2] + "<br>"); // Outputs: l document.write(chars[chars.length - 1]); // Outputs: ! </script> </body> </html>