JavaScript Access character at index in string


Example

Use charAt() to get a character at the specified index in the string.

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

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

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

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

var string = "Hello, World!";
console.log( string.charCodeAt(4) ); // 111

Note that these methods are all getter methods (return a value). Strings in JavaScript are immutable. In other words, none of them can be used to set a character at a position in the string.