javascript エラー「RangeError: Arguments contain a value that is out of range of code points」の解決方法

javascript エラー「RangeError: Arguments contain a value that is out of range of code points」の解決方法

javascriptで、エラー「RangeError: Arguments contain a value that is out of range of code points」が発生した場合の原因と解決方法を記述してます。「fromCodePoint」に存在しない「Unicodeコードポイント」を指定した場合に発生します。

環境

  • OS macOS Monterey
  • ブラウザ safari 15.5

エラー内容

以下のUnicodeコードポイントから文字を生成するコードで発生。

String.fromCodePoint(-1)

エラーメッセージ

RangeError: Arguments contain a value that is out of range of code points

画像

原因

「-1」は「fromCodePoint」に使用できる「Unicodeコードポイント」ではないため

解決方法

使用できる「Unicodeコードポイント」にする

console.log(
  String.fromCodePoint(97) // a
)