The syntax of the fromCodePoint()
method is:
String.fromCodePoint(num1, ..., numN)
The fromCodePoint()
method, being a static method, is called using the String
class name.
fromCodePoint() Parameters
The fromCodePoint()
method takes in :
- num1, ..., numN - A sequence of code points.
Return value from fromCodePoint()
- Returns a string created by using the specified sequence of code points.
Notes:
- The
fromCodePoint()
method throws aRangeError
if an invalid Unicode code point is given. - The
fromCodePoint()
method returns a string and not aString
object.
Example: Using fromCodePoint() method
let string1 = String.fromCodePoint(65, 66, 67);
console.log(string1); // ABC
let string2 = String.fromCharCode(72, 69, 76, 76, 79);
console.log(string2); // HELLO
// numbers can be passed as hexadecimals
let string3 = String.fromCodePoint(0x2f804);
console.log(string3); // "\uD87E\uDC04"
// unlike fromCharCode() that requires surrogate pair to return supplementary char
// fromCodePoint() can even return 4-byte supplementary chars
let string4 = String.fromCodePoint(0x1f303);
console.log(string4); // Unicode character "Night with Stars"
let string5 = String.fromCodePoint(Infinity);
console.log(string5); // RangeError
Output
ABC HELLO \uD87E\uDC04 <unicode Night with Stars> RangeError: Invalid code point Infinity
Recommended Reading: JavaScript String fromCharCode()