JavaScript String.fromCharCode()
Syntax & Examples


String.fromCharCode() static-method

The `fromCharCode` method in JavaScript returns a string created by using the specified sequence of Unicode values.


Syntax of String.fromCharCode()

The syntax of String.fromCharCode() static-method is:

String.fromCharCode()
String.fromCharCode(num1)
String.fromCharCode(num1, num2)
String.fromCharCode(num1, num2, /* …, */ numN)

This fromCharCode() static-method of String returns a string created by using the specified sequence of Unicode values.

Parameters

ParameterOptional/RequiredDescription
num1, num2, ... , numNrequiredA sequence of Unicode values representing characters to include in the string.

Return Type

String.fromCharCode() returns value of type String.



✐ Examples

1 Character for Unicode value 65

In this example,

  1. We use the fromCharCode() method with the Unicode value 65.
  2. This represents the character 'A' in the ASCII table.
  3. We then log the result to the console.

JavaScript Program

void main() {
  let numCode = String.fromCharCode(65);
  console.log('Character for Unicode 65:', numCode);
}

Output

Character for Unicode 65: A

2 Characters for Unicode values 65, 66, 67

In this example,

  1. We use the fromCharCode() method with Unicode values 65, 66, and 67.
  2. These represent the characters 'A', 'B', and 'C' respectively in the ASCII table.
  3. We then log the result to the console.

JavaScript Program

void main() {
  let charCodes = String.fromCharCode(65, 66, 67);
  console.log('Characters for Unicode 65, 66, 67:', charCodes);
}

Output

Characters for Unicode 65, 66, 67: ABC

3 Mixed Characters for Unicode values

In this example,

  1. We use the fromCharCode() method with a mix of Unicode values.
  2. These values represent the characters 'A', 'B', 'C', 'a', 'b', and 'c' in the ASCII table.
  3. We then log the result to the console.

JavaScript Program

void main() {
  let mixedCodes = String.fromCharCode(65, 66, 67, 97, 98, 99);
  console.log('Mixed Characters:', mixedCodes);
}

Output

Mixed Characters: ABCabc

Summary

In this JavaScript tutorial, we learned about fromCharCode() static-method of String: the syntax and few working examples with output and detailed explanation for each example.