- JavaScript String charCodeAt()
- See Also:
- charCodeAt() vs codePointAt()
- Syntax
- Parameters
- Return Value
- More Examples
- Related Pages
- Browser Support
- String.prototype.charCodeAt()
- Try it
- Syntax
- Parameters
- Return value
- Description
- Examples
- Using charCodeAt()
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- KeyboardEvent: charCode property
- Value
- Examples
- HTML
- JavaScript
- Result
- Notes
- Specifications
- Browser compatibility
- Found a content problem with this page?
- Raiseupwa.com
- How many ASCII characters are there?
- What is the Ascii code for the letter A?
- What is JavaScript character code?
- What is ASCII example?
- What is the purpose of an ASCII code?
- What is ASCII code 10?
JavaScript String charCodeAt()
The charCodeAt() method returns the Unicode of the character at a specified index (position) in a string.
The index of the first character is 0, the second is 1, .
The index of the last character is string length — 1 (See Examples below).
See Also:
charCodeAt() vs codePointAt()
charCodeAt() is UTF-16, codePointAt() is Unicode.
charCodeAt() returns a number between 0 and 65535.
Both methods return an integer representing the UTF-16 code of a character, but only codePointAt() can return the full value of a Unicode value greather 0xFFFF (65535).
For more information about Unicode Character Sets, visit our Unicode Reference.
Syntax
Parameters
Return Value
Type | Description |
A number | The Unicode of the character at the specified index. NaN if the index is invalid. |
More Examples
Get the Unicode of the last character in a string:
Get the Unicode of the 15th character:
Related Pages
Browser Support
charCodeAt() is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
String.prototype.charCodeAt()
The charCodeAt() method of String values returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
charCodeAt() always indexes the string as a sequence of UTF-16 code units, so it may return lone surrogates. To get the full Unicode code point at the given index, use String.prototype.codePointAt() .
Try it
Syntax
Parameters
Zero-based index of the character to be returned. Converted to an integer — undefined is converted to 0.
Return value
An integer between 0 and 65535 representing the UTF-16 code unit value of the character at the specified index . If index is out of range of 0 – str.length — 1 , charCodeAt() returns NaN .
Description
Characters in a string are indexed from left to right. The index of the first character is 0 , and the index of the last character in a string called str is str.length — 1 .
Unicode code points range from 0 to 1114111 ( 0x10FFFF ). charCodeAt() always returns a value that is less than 65536 , because the higher code points are represented by a pair of 16-bit surrogate pseudo-characters. Therefore, in order to get a full character with value greater than 65535 , it is necessary to retrieve not only charCodeAt(i) , but also charCodeAt(i + 1) (as if manipulating a string with two characters), or to use codePointAt(i) instead. For information on Unicode, see UTF-16 characters, Unicode code points, and grapheme clusters.
Examples
Using charCodeAt()
The following example returns 65 , the Unicode value for A.
"ABC".charCodeAt(0); // returns 65
charCodeAt() may return lone surrogates, which are not valid Unicode characters.
const str = "𠮷𠮾"; console.log(str.charCodeAt(0)); // 55362, or d842, which is not a valid Unicode character console.log(str.charCodeAt(1)); // 57271, or dfb7, which is not a valid Unicode character
To get the full Unicode code point at the given index, use String.prototype.codePointAt() .
const str = "𠮷𠮾"; console.log(str.codePointAt(0)); // 134071
Note: Avoid re-implementing codePointAt() using charCodeAt() . The translation from UTF-16 surrogates to Unicode code points is complex, and codePointAt() may be more performant as it directly uses the internal representation of the string. Install a polyfill for codePointAt() if necessary.
Below is a possible algorithm to convert a pair of UTF-16 code units into a Unicode code point, adapted from the Unicode FAQ:
// constants const LEAD_OFFSET = 0xd800 - (0x10000 >> 10); const SURROGATE_OFFSET = 0x10000 - (0xd800 10) - 0xdc00; function utf16ToUnicode(lead, trail) return (lead 10) + trail + SURROGATE_OFFSET; > function unicodeToUTF16(codePoint) const lead = LEAD_OFFSET + (codePoint >> 10); const trail = 0xdc00 + (codePoint & 0x3ff); return [lead, trail]; > const str = "𠮷"; console.log(utf16ToUnicode(str.charCodeAt(0), str.charCodeAt(1))); // 134071 console.log(str.codePointAt(0)); // 134071
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 3, 2023 by MDN contributors.
Your blueprint for a better internet.
KeyboardEvent: charCode property
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
The charCode read-only property of the KeyboardEvent interface returns the Unicode value of a character key pressed during a keypress event.
Warning: Do not use this property, as it is deprecated. Instead, get the Unicode value of the character using the key property.
Value
A number that represents the Unicode value of the character key that was pressed.
Examples
HTML
p>Type anything into the input box below to log a code>charCodecode>.p> input type="text" /> p id="log">p>
JavaScript
const input = document.querySelector("input"); const log = document.querySelector("#log"); input.addEventListener("keypress", (e) => log.innerText = `Key pressed: $String.fromCharCode(e.charCode)>\ncharCode: $ e.charCode >`; >);
Result
Notes
- In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, but never both. If the key pressed generates a character (e.g., ‘a’), charCode is set to the code of that character; charCode respects the letter case (in other words, charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode .
- When one or more modifier keys are pressed, there are some complex rules for charCode . See Gecko Keypress Event for details.
- charCode is never set in the keydown and keyup events. In these cases, keyCode is set instead.
- To get the code of the key regardless of whether it was stored in keyCode or charCode , query the which property.
- Characters entered through an IME do not register through keyCode or charCode .
- For a list of the charCode values associated with particular keys, run Example 7: Displaying Event Object Properties and view the resulting HTML table.
Specifications
Browser compatibility
BCD tables only load in the browser
Found a content problem with this page?
This page was last modified on Apr 7, 2023 by MDN contributors.
Your blueprint for a better internet.
Raiseupwa.com
In JavaScript, charCodeAt() is a string method that is used to retrieve a Unicode value for a character at a specific position in a string. Because the charCodeAt() method is a method of the String object, it must be invoked through a particular instance of the String class.
What is ASCII code?
The standard ASCII code uses seven-digit binary numbers; i.e., numbers consisting of various sequences of 0’s and 1’s. The code can represent 128 different characters, since there are 128 different possible combinations of seven 0’s and 1’s.
How many ASCII characters are there?
256 different characters
A computer system normally stores characters using the ASCII code. Each character is stored using eight bits of information, giving a total number of 256 different characters (2**8 = 256).
What is the Ascii code for the letter A?
065
ASCII – Binary Character Table
Letter | ASCII Code | Binary |
---|---|---|
A | 065 | 01000001 |
B | 066 | 01000010 |
C | 067 | 01000011 |
D | 068 | 01000100 |
Is a number JavaScript?
In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.
What is JavaScript character code?
Unicode code points range from 0 to 1114111 ( 0x10FFFF ). The first 128 Unicode code points are a direct match of the ASCII character encoding. (For information on Unicode, see the JavaScript Guide.) Note: charCodeAt() will always return a value that is less than 65536 .
What is ASCII example?
Pronounced ask-ee, ASCII is the acronym for the American Standard Code for Information Interchange. It is a code for representing 128 English characters as numbers, with each letter assigned a number from 0 to 127. For example, the ASCII code for uppercase M is 77.
Is ASCII a programming language?
Short for American Standard Code for Information Interexchange, ASCII is a standard that assigns letters, numbers, and other characters in the 256 slots available in the 8-bit code. The ASCII decimal (Dec) number is created from binary, which is the language of all computers. Convert text into ASCII.
What is the purpose of an ASCII code?
What is ASCII code 10?
How many ASCII code are there?