javascript エラー「Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions」の解決方法
- 作成日 2022.07.05
- javascript
- javascript
![javascript エラー「Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions」の解決方法](https://mebee.info/wp-content/uploads/2022/02/javascript-1-890x500.png)
javascriptで、エラー「Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions」が発生した場合の原因と解決方法を記述してます。
環境
- OS windows11 pro 64bit
- Apache 2.4.43
- ブラウザ chrome 103.0.5060.66
エラー内容
以下のコードで発生。
console.log( 2n + 2n + 1 )
エラーメッセージ
Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
画像
![](https://mebee.info/wp-content/uploads/2022/03/image-471.png)
firefox(バージョン102)では、以下のエラーとなります。
Uncaught TypeError: can't convert BigInt to number
画像
![](https://mebee.info/wp-content/uploads/2022/03/image-472.png)
原因
BigIntは、BigInt同士でないと演算できないため
解決方法
全てBigIntで演算するか、
console.log( 2n + 2n + 1n ) // 5n
キャストして演算する
console.log( Number(2n) + Number(2n) + 1 ) // 5
-
前の記事
Spyder5 日本語化を行う 2022.07.05
-
次の記事
Linux テキストデータを後ろから表示する 2022.07.05
コメントを書く