totn JavaScript

JavaScript: String fromCodePoint() method

This JavaScript tutorial explains how to use the string method called fromCodePoint() with syntax and examples.

Description

In JavaScript, fromCodePoint() is a string method that is used to create a string from a sequence of Unicode code points (that may not be representable in a single UTF-16 code unit). Because the fromCodePoint() method is a static method of the String constructor, it must be invoked through the String constructor object rather than through the particular instance of the String class.

Syntax

In JavaScript, the syntax for the fromCodePoint() method is:

String.fromCodePoint(value1, value2, ... value_n);

Parameters or Arguments

value1, value2, ... value_n
The Unicode code points to convert to characters and then concatenate together into a string.

Returns

The fromCodePoint() method accepts a sequence of Unicode code points, converts these values to characters and concatenates them together in a string. This resulting string is then returned by the fromCodePoint() method.

The fromCodePoint() method will return RangeError if an invalid code point is provided as a parameter.

Note

  • The fromCodePoint() method does not change the value of the original string.
  • You can also use the fromCharCode() method if all of the Unicode values are representable in single UTF-16 code units.

Example

Let's take a look at an example of how to use the fromCodePoint() method in JavaScript.

For example:

console.log(String.fromCodePoint(134071));

In this example, we have invoked the fromCodePoint() static method through the String constructor object.

We have written the output of the fromCodePoint() method to the web browser console log, for demonstration purposes, to show what the fromCodePoint() method returns.

The following will be output to the web browser console log:

𠮷

As you can see, the fromCodePoint() method converted the Unicode code point of 134071 into the character value '𠮷'.