在JavaScript中,当你乘法运算时,
1 * 0 = 0
,而-1 * 0 = -0
。我可以理解它们在技术上是相同的,但是它们在底层有所不同吗?1 * 0 = 0
,而-1 * 0 = -0
。我可以理解它们在技术上是相同的,但是它们在底层有所不同吗?0
或-0
)。const a = 0;
const b = -0;
a == b; // true
a === b; // true
a < b; // false
b < a; // false
Object.is(a, b); // false
Object.is(a, -b); // true
b.toString(); // "0" <-- loses the negative sign
a + b; // 0
b - a; // -0
a * b; // -0
是的,它们有所不同。JavaScript拥有带符号的零,因此在内部表示时两者是不同的。
还有一些实际上的区别:
console.log(1 / +0 === Infinity) // true
console.log(1 / -0 === -Infinity) // true
float === float
的原因。 - Keith