JavaScript/0n
0n
0n
は、JavaScriptにおけるBigInt型の値の一つで、BigInt型のゼロを表します。BigInt型の数値演算や条件評価など、多くの場面で使用される基本的な値です。
特徴
使用例
const bigIntZero = 0n; console.log(typeof bigIntZero); // "bigint" console.log(bigIntZero + 1n); // 1n console.log(bigIntZero === 0n); // true
0nの特別な性質
0nは、BigInt型におけるゼロの値です。
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
されます。