To convert a character to its ASCII code (or UTF-16 code unit, which matches ASCII for standard characters 0–127) in JavaScript using charCodeAt(), call str.charCodeAt(index) (defaulting to index 0 on a single-character string) to return an integer from 0 to 65535. Because charCodeAt() reads a 16-bit UTF-16 code unit rather than a full Unicode code point, it returns the exact ASCII number for basic Latin characters ("A".charCodeAt(0) is 65), while returning NaN if the index is out of bounds or the string is empty.
1. Basic Character-to-ASCII Conversion with charCodeAt()
Call .charCodeAt(0) on a string to get the numeric character code of the first character, and verify the input before using the result:
// Convert the first character of a string to its ASCII / UTF-16 code unit
function charToAscii(char) {
if (typeof char !== "string" || char.length === 0) {
throw new TypeError("Expected a non-empty string.");
}
return char.charCodeAt(0);
}
// Example usage:
console.log(charToAscii("A")); // 65
console.log(charToAscii("a")); // 97
console.log(charToAscii("0")); // 48
console.log(charToAscii(" ")); // 32
- Index defaults to
0if omitted: Calling"Z".charCodeAt()is equivalent to"Z".charCodeAt(0), returning90. - Out-of-range index returns
NaN: Calling"abc".charCodeAt(10)or"".charCodeAt(0)returnsNaNrather than throwing.
2. Converting Every Character in a String to ASCII Codes
To inspect or transform all characters in a string, iterate over its indices with a loop or Array.from() and collect each character’s charCodeAt(i) value:
// Map each character in a string to its code unit
function stringToAsciiCodes(text) {
if (typeof text !== "string") {
throw new TypeError("Expected text to be a string.");
}
return Array.from(text, (_, index) => text.charCodeAt(index));
}
// Example usage:
console.log(stringToAsciiCodes("Hi!")); // [72, 105, 33]
- Helpful for character ranges: Checking
code >= 48 && code <= 57tests ASCII digits ('0'–'9'),65–90covers'A'–'Z', and97–122covers'a'–'z'. - Reverse conversion: Pass a code back into
String.fromCharCode(65)to get"A".
3. ASCII (0–127) vs. Unicode Characters Outside ASCII
ASCII defines 128 characters (0–127), while JavaScript strings use UTF-16, meaning non-ASCII characters return values above 127 and surrogate pairs require codePointAt() if you need full Unicode code points:
// ASCII vs. extended/non-ASCII characters
console.log("~".charCodeAt(0)); // 126 (within standard ASCII 0-127)
console.log("é".charCodeAt(0)); // 233 (outside 7-bit ASCII)
// For astral symbols / emoji (outside BMP), codePointAt(0) reads full code point
const grin = "😀";
console.log(grin.charCodeAt(0)); // 55357 (high surrogate half)
console.log(grin.codePointAt(0)); // 128512 (full Unicode code point)
- ASCII boundary check: If your logic strictly expects standard ASCII, verify
code >= 0 && code <= 127. - Surrogate pairs: Emoji and rare symbols take two UTF-16 code units, so
codePointAt(0)is safer thancharCodeAt(0)when handling full Unicode text.
TL;DR: Call char.charCodeAt(0) on a non-empty string to get its ASCII/UTF-16 numeric code (such as 65 for "A" and 97 for "a"), check for NaN on empty/out-of-bounds indices, and verify code <= 127 when enforcing strict ASCII.
