String.fromCodePoint()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
String.fromCodePoint()
静的メソッドは指定されたコードポイントの並びを使って生成された文字列を返します。
試してみましょう
console.log(String.fromCodePoint(9731, 9733, 9842, 0x2f804));// Expected output: "☃★♲你"
構文
String.fromCodePoint(num1)String.fromCodePoint(num1, num2)String.fromCodePoint(num1, num2, /* …, */ numN)
引数
numN
0
以上0x10FFFF
以下の整数で、Unicode コードポイントを表します。
返値
指定されたコードポイントの並びを使って生成された文字列です。
例外
RangeError
numN
を数値に変換した後で整数でなかった場合、0
よりも小さかった場合、0x10FFFF
よりも大きかった場合に発生します。
解説
fromCodePoint()
はString
の静的メソッドなので、自分で生成したString
オブジェクトのメソッドではなく、常にString.fromCodePoint()
として使用するようにしてください。
Unicode のコードポイントは0
から1114111
(0x10FFFF
) までの範囲です。charAt()
は常に65536
より小さい値を返しますが、これは上位のコードポイントは 16 ビットのサロゲート擬似文字のペアによって表されているからです。従って、fromCodePoint()
は(UTF-16 コード単位である)length
が渡された引数の数よりも大きくなる文字列を返す可能性があります。Unicode に関する情報はUTF-16 文字、Unicode コードポイント、書記素クラスターを参照してください。
例
fromCodePoint() の使用
有効な入力:
String.fromCodePoint(42); // "*"String.fromCodePoint(65, 90); // "AZ"String.fromCodePoint(0x404); // "\u0404" === "Є"String.fromCodePoint(0x2f804); // "\uD87E\uDC04"String.fromCodePoint(194564); // "\uD87E\uDC04"String.fromCodePoint(0x1d306, 0x61, 0x1d307); // "\uD834\uDF06a\uD834\uDF07"
無効な入力:
String.fromCodePoint("_"); // RangeErrorString.fromCodePoint(Infinity); // RangeErrorString.fromCodePoint(-1); // RangeErrorString.fromCodePoint(3.14); // RangeErrorString.fromCodePoint(3e-2); // RangeErrorString.fromCodePoint(NaN); // RangeError
fromCharCode() との比較
String.fromCharCode()
では補助文字(すなわち0x010000
–0x10FFFF
)のコードポイントを指定することで返すことができません。補助文字を返すためには、 UTF-16 のサロゲートペアでが必要です。
String.fromCharCode(0xd83c, 0xdf03); // Code Point U+1F303 "Night withString.fromCharCode(55356, 57091); // Stars" === "\uD83C\uDF03"
一方、String.fromCodePoint()
はコードポイント(UTF-32 のコード単位に相当)を指定することで、 4 バイトの補助文字や、一般的な 2 バイトの BMP 文字を返すことができます。
String.fromCodePoint(0x1f303); // または 10 進数で 127747
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-string.fromcodepoint |