JavaScript String charAt()
- Página anterior at()
- Página siguiente charCodeAt()
- Volver a la capa superior Manual de referencia de JavaScript String
Definition and usage
charAt()
The method returns the character at the specified index (subscript) in the string.
The index of the first character is 0, the second is 1, ...
The index of the last character is string length - 1 (see the example below the page).
See also:
Instance
Example 1
Get the first character in the string:
let text = "HELLO WORLD"; let letter = text.charAt(0);
Example 2
Get the second character in the string:
let text = "HELLO WORLD"; let letter = text.charAt(1);
Example 3
Get the last character in the string:
let text = "HELLO WORLD"; let letter = text.charAt(text.length-1);
Example 4
Indexes out of range return an empty string:
let text = "HELLO WORLD"; let letter = text.charAt(15);
Example 5
The default index is 0:
let text = "HELLO WORLD"; let letter = text.charAt();
Example 6
Invalid index converted to 0:
let text = "HELLO WORLD"; let letter = text.charAt(3.14);
Syntax
string.charAt(n)
Parameter
Parameter | Description |
---|---|
n | Required. Number. The index (subscript) of the character. |
Return value
Type | Description |
---|---|
String |
Specifies the character at the index. If the index is invalid, it is an empty string (""). |
Description
If the parameter n Not between 0 and stringIf the index is between .length-1, the method returns an empty string.
Note:JavaScript does not have a character data type different from string types, so the returned character is a string of length 1.
Browser support
charAt()
It is an ECMAScript1 (ES1) feature.
All browsers fully support ES1 (JavaScript 1997):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Soporte | Soporte | Soporte | Soporte | Soporte | Soporte |
- Página anterior at()
- Página siguiente charCodeAt()
- Volver a la capa superior Manual de referencia de JavaScript String