Access character at index in string

suggest change

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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Strings:
* Syntax
* Access character at index in string

Table Of Contents
8 Strings
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde