0

How to acccess character in JavaScript at specified index in string

To get a character at the specified index in the string use charAt function.

var string = "Hello, World!";
console.log( string.charAt(4) ); // "o"
RUN

Alternatively, because strings can be treated like arrays, use the index via bracket notation

var string = "Hello, World!";
console.log( string[4] ); // "o"
RUN

To get the character code of the character at a specified index, use charCodeAt().

var string = "Hello, World!";
console.log( string.charCodeAt(4) ); // 111
RUN
add comment
Suggest a different solution or add another example