JS: String.fromCharCode

By Xah Lee. Date: . Last updated: .

String.fromCharCode ( …codeUnits )

Return a string, where each character in string has corresponding codeUnits.

Each of codeUnit should be a integer from 0 to 65535 inclusive. (Note: 65535 = 2^16 -1). If it is greater than 65535, a value of modulo(codeUnit, 2^16) is used.

(you cannot use String.fromCharCode() to create a string that contains a character with Unicode codepoint greater than 65535.)

Note: you should use String.fromCodePoint() instead.

[see JS: String.fromCodePoint]

[see JS: String Code Unit vs Code Point]

// convert sequence of unicode code point to string
let s = String.fromCharCode ( 97, 98);
console.log ( s === "ab" ); // true
// 97 is the code point for letter “a”
// 98 is the code point for letter “b”
// String.fromCharCode won't work as expected if code point is greater than 65535 (that's 2^16 )

let t = String.fromCharCode ( 128514 );

// 😂
// name: FACE WITH TEARS OF JOY
// codepoint decimal: 128514
// codepoint hexadecimal: 1f602

console.log ( t ); // prints , WRONG

Reference

ECMAScript 2015 §Text Processing#sec-string.fromcharcode

Character Topic

  1. JS: String Code Unit vs Code Point
  2. JS: Convert Character To/From Codepoint
  3. JS: String.fromCodePoint
  4. JS: String.fromCharCode
  5. JS: String.prototype.charAt
  6. JS: String.prototype.charCodeAt
  7. JS: String.prototype.codePointAt
  8. JS: Convert Decimal/Hexadecimal
  9. JS: Unicode Character Escape Sequence

String Topic

  1. JS: Source Code Charset/Encoding
  2. JS: String Code Unit vs Code Point
  3. JS: Unicode Character Escape Sequence
  4. JS: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML/XML Entity List

  1. JS: Template String
  2. JS: Convert String to Number
  3. JS: Encode URL, Escape String
  4. JS: Format Number
  5. JS: JSON Object

  1. JS: String Object
  2. JS: String.prototype
  3. JS: String Constructor
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon