Q:

How to Split a String into an Array of Characters in JavaScript

0

How to Split a String into an Array of Characters in JavaScript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the split() Method

The JavaScript split() method is used to split a string using a specific separator string, for example, comma (,), space, etc. However, if the separator is an empty string (""), the string will be converted to an array of characters, as demonstrated in the following example:

<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
alert(chars[0]); // Outputs: H
alert(chars[1]); // Outputs: e
alert(chars[2]); // Outputs: l
alert(chars[chars.length - 1]); // Outputs: !
</script>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Dynamically Access Object Property Using Va... >>
<< How to Parse JSON in JavaScript...