The String charCodeAt() method
The charCodeAt()
method returns the Unicode of the character at a specified index (position) in a string.
Syntax
str.charCodeAt(index)
- Here
str
is a string
Parameters
The charCodeAt()
method takes in
index
– (Required) – A Number – The index (position) of the character.- If number is not provided, the default value is used.
Return Value
- A number – The Unicode of the character at the specified index.
charCodeAt()
returnsNaN
if index is negative or out of range.
Examples
Get the Unicode of the first character in a string
const text = "Hello World"; const letter = text.charCodeAt(0); console.log(letter)
Output
72
Get the Unicode of the Third character in a string
const text = "Hello World"; const letter = text.charCodeAt(2); console.log(letter)
Output
108
Get the Unicode of the Last character in a string
const text = "Hello World"; const letter = text.charCodeAt(text.length - 1); console.log(letter)
Output
100