JavaScript String charAt()
❮
Reference
❯
Examples
Get the first character in a string:
let text = "HELLO WORLD";
let letter = text.charAt(0);
Try it Yourself »
Get the second character in a string:
let text = "HELLO WORLD";
let letter = text.charAt(1);
Try it Yourself »
More examples below.
Definition and Usage
The charAt()
method returns the character at a specified index (position) in a string.
The index of the first character is 0, the second 1, ...
The index of the last character is string length - 1 (See Examples below).
See also the charCodeAt()
method.
Syntax
string.charAt(index)
Parameters
Parameter | Description |
index | Required. A number. The index (position) of the character. |
Return Value
Type | Description |
A string | The character at the specified index. An empty string ("") if the index is invalid. |
More Examples
Get the last character in a string:
let text = "HELLO WORLD";
let letter = text.charAt(text.length-1);
Try it Yourself »
Get the 15th character in a string:
let text = "HELLO WORLD";
let letter = text.charAt(15);
Try it Yourself »
Browser Support
charAt()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
❮
Reference
❯