JS: String.fromCharCode

By Xah Lee. Date: . Last updated: .

String.fromCharCode ( codeUnit1, codeUnit2 … )

Return a string, where each character in string has corresponding code units.

Example:

String.fromCharCode ( 97, 98) === "ab"

[see JS: Character, Code Unit, Codepoint]

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.

(it's possible to use String.fromCharCode() to create a string that contains a character with Unicode codepoint greater than 65535. You have to find the pair of 16 bits values (called surrogate pairs) to form 1 such character.)

Note: most of the time, you probably want String.fromCodePoint() instead.

[see JS: String.fromCodePoint]

// convert sequence of unicode codepoint to string

console.log (
    String.fromCharCode ( 97, 98) === "ab"
); // true

// 97 is the codepoint for letter “a”
// 98 is the codepoint for letter “b”
// String.fromCharCode won't work as expected if codepoint is greater than 65535 (that's 2^16 )

const tt = String.fromCharCode ( 128514 );

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

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

JS Character

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

JS String

  1. String Overview
  2. Template String
  3. Char, Code Unit, Codepoint
  4. String Escape Sequence
  5. Unicode Escape Sequence
Liket it? I spend 2 years writing this tutorial. Help me spread it. Tell your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Object Ref
  5. DOM Scripting
  6. SVG
  7. Blog