JavaScript String fromCodePoint() Method
The JavaScript String fromCodePoint() method returns a string created by a specified sequence of code point values. You can pass one or more Unicode code point values to this method. If the value passed is not an integer, less than 0 (or negative), or greater than 0x10FFFF, it will throw a RangeError exception. This method is static and can be invoked using the class syntax, such as String.fromCodePoint(), rather than calling it on a variable.
A Unicode code point is a numeric value that corresponds to a specific character in the Unicode standard. Each character and symbol has its own unique Unicode code point value.
Syntax
Following is the syntax of JavaScript String fromCodePoint() method −
String.fromCodePoint(num1, num2, /*..., */ numN)
Parameters
This method accepts multiple parameters with the same type, such as 'num1', 'num2', 'num3' up to 'numN', which are described below −
- num1, num2....numN − It is an integer value representing Unicode code points within the range of [0, 0x10FFFF].
Return value
This method returns a string created by the specified code point sequence.
Example 1
If we pass a single Unicode code point value to this method, it will return a string with only one character.
In the following example, we are using the JavaScript String fromCodePoint() method to retrieve a string created by the specified Uni code point value 65.
<html>
<head>
<title>JavaScript String fromCodePoint() Method</title>
</head>
<body>
<script>
const unicodepoint = 65;
document.write("Uni code point: ", unicodepoint);
document.write("<br>The uni code point ", unicodepoint, " represents ", String.fromCodePoint(unicodepoint));
</script>
</body>
</html>
Output
The above program returns a string "A".
Uni code point: 65 The uni code point 65 represents A
Example 2
If multiple Unicode values are passed, the method returns a string containing multiple characters.
The following is another example of the JavaScript String fromCodePoint() method. We use this method to retrieve a string containing multiple characters created by the specified uni code sequence 84, 117, 116, 111, 114, 105, 97, 97, 108, 115, 80, 111, 105, 110, 116.
<html>
<head>
<title>JavaScript String fromCodePoint() Method</title>
</head>
<body>
<script>
const num1 = 84;
const num2 = 117;
const num3 = 116;
const num4 = 111;
const num5 = 114;
const num6 = 105;
const num7 = 97;
const num8 = 108;
const num9 = 115;
const num10 = 80;
const num11 = 111;
const num12 = 105;
const num13 = 110;
const num14 = 116;
document.write("Uni code point values are: ", num1, ", ", num2, ", ", num3, ", ", num4, ", ", num5, ", ", num6, ", ", num7, ", ", num7, ", ", num8, ", ", num9, ", ", num10, ", ", num11, ", ", num12, ", ", num13, ", ", num14);
document.write("<br>Created string: ", String.fromCodePoint(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14));
</script>
</body>
</html>
Output
After executing the above program, it will return a new string as "TutorialsPoint".
Uni code point values are: 84, 117, 116, 111, 114, 105, 97, 97, 108, 115, 80, 111, 105, 110, 116 Created string: TutorialsPoint
Example 3
If the numN is not an integer, less than zero, or greater than the 0x10FFFF, it will throw a 'RangeError' exception.
<html>
<head>
<title>JavaScript String fromCodePoint() Method</title>
</head>
<body>
<script>
const unicodepoint = -2;
document.write("Uni code point: ", unicodepoint);
try {
document.write("Created string: ", String.fromCodePoint(unicodepoint));
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
Output
Once the above program is executed, it will throw a 'RangeError' exception.
Uni code point: -2 RangeError: Invalid code point -2
Example 4
In the given example below, we use the fromCodePoint() method within the from loop to retrieve a string containing all capital letters created by the passed number one by one to this method, and the loop starts from 65 and iterates till 90.
<html>
<head>
<title>JavaScript String fromCodePoint() Method</title>
</head>
<body>
<script>
const unicodepoint_start = 65;
document.write("Unicode starts at: ", unicodepoint_start);
document.write("<br>New created string: ");
for(let i = unicodepoint_start; i<=90; i++){
document.write(String.fromCodePoint(i));
}
</script>
</body>
</html>
Output
The above program returns a string containing all capital letters as −
Unicode starts at: 65 New created string: ABCDEFGHIJKLMNOPQRSTUVWXYZ