JavaScript/0n
表示
0n
[編集]0n
は、JavaScriptにおけるBigInt型の値の一つで、BigInt型のゼロを表します。BigInt型の数値演算や条件評価など、多くの場面で使用される基本的な値です。
特徴
[編集]0n
はBigInt型のプリミティブ型。- BigInt型の数値演算において中立的な値として機能します(加算では結果に影響を与えない)。
- 真偽値評価ではfalsyと見なされます。
- 任意の精度の整数を表現できます。
使用例
[編集]const bigIntZero = 0n; console.log(typeof bigIntZero); // "bigint" console.log(bigIntZero + 1n); // 1n console.log(bigIntZero === 0n); // true
0nの特別な性質
[編集]0nは、BigInt型におけるゼロの値です。
0nと真偽値評価
[編集]JavaScriptにおいて、0n
はfalsyとして評価されます。
if (0n) { console.log("このコードは実行されません"); } else { console.log("0nはfalsyです"); // 実行される }
0nの型変換
[編集]0n
は、コンテキストに応じて他の型に変換されることがあります。
文字列への変換
[編集]console.log(String(0n)); // "0" console.log(typeof String(0n)); // "string"
真偽値への変換
[編集]console.log(Boolean(0n)); // false
注意点
[編集]console.log(0n == false); // true console.log(0n === false); // false
- 同じ式で BigInt型と通常の数値型が混在するしていると、
TypeError: Cannot mix BigInt and other types, use explicit conversions
がthrow
されます。