javascript 真实数字
javascript truthy numbers
基于这些规则:
错误:
- 错误
- 0(零)
- '' 或 ""(空字符串)
- 无
- 未定义
- NaN(例如 1/0 的结果)
真实:其他一切
我找不到正确的解释,说明为什么在接下来的测试中,只有数字 1 的计算结果为 "true"
0 == true ("false")
1 == true ("true")
2 == true ("false")
othernumber == true ("false")
"truthy" 和 "falsy" 规则仅在值本身用作测试时适用,例如:
var str = "";
if (str) {
// It's truthy
} else {
// It's falsy
}
==
有自己的一套不同的规则来确定其操作数的松散相等性,这些规则在规范的 Abstract Equality Comparison algorithm:
中有详尽的解释
- If Type(x) is the same as Type(y), then
- Return the result of performing Strict Equality Comparison x === y.
- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
- If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
- If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
- If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
- If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
- Return false.
有关其中列出的各种抽象操作的完整详细信息,请参阅规范,尽管名称几乎说明了它们的作用。 (如果你查看规范,你会在不同的地方看到 ToNumber
之前的 !
;我已经在上面删除了它。它是 not 逻辑 NOT运算符,它是 a spec notation related to "abrupt completions.")
让我们按照您的 2 == true
示例进行操作:
- 类型不一样,继续
- x 不为空,所以继续
- x 不是未定义的,所以继续
- Type(x) 确实是 Number,但 Type(y) 不是 String,所以继续
- Type(x) 不是字符串,所以继续
- Type(x) 不是布尔值,所以继续
- Type(y) 是布尔值,所以 return x == ToNumber(y) 的结果
- ToNumber(true) 为 1,由于 2 == 1 为假,结果为假
但请注意,您的 1 == true
示例的第 7 步有所不同:
- Type(y) 是布尔值,所以 return x == ToNumber(y) 的结果
- ToNumber(true) 为 1,由于 1 == 1 为真,所以结果为真
使用 ==
不同于 if(something)
此测试将为您提供预期结果:
function truthyFalsyTest()
{
if(0)
{
console.log("true");
}
else
{
console.log("false");
}
if(1)
{
console.log("true");
}
else
{
console.log("false");
}
if(2)
{
console.log("true");
}
else
{
console.log("false");
}
if(2222)
{
console.log("true");
}
else
{
console.log("false");
}
}
truthyFalsyTest();
这是一个非常有趣的原因,根据 this book,当您将任何东西与布尔值进行比较时,例如在 x == y
中,它遵循以下模式:
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
因此,当您比较 1 == true
时,它实际上在做 1 == ToNumber(true)
,然后变成 1 == 1
,但是当您比较 2 == true
时,它变成 2 == 1
这当然是错误的。
这和书中给出的其他一些原因建议不要将事物与布尔值进行比较。
当您与布尔值进行比较时,值本身是真值还是假值并不重要,因为它永远不会变成布尔值,但布尔值会被强制转换为可以与之比较的类型==
运算符的另一端。
希望您对这个回答满意。
在JavaScript中(==)是一个相等运算符运算符,在这里完成类型转换。更严格的(===)恒等运算符不会做类型转换。
例如,即使数字不是布尔值,如果使用 (==) 运算符,您也可以在需要布尔值的地方使用数值。
但是,如果您执行更严格的 (===) 运算符,您会看到“1 === true”的计算结果为 false。
Based on these rules:
The issue here is that ==
does NOT operate by these rules. Whether something is truthy or not and how it behaves during equality tests are two different things. For the record a more correct test for truthyness would be
if (value)
return true;
else
return false;
甚至更短 - 直接转换 Boolean(value)
(以及隐式转换 !!value
.
但是,在相等性测试期间,==
的两边将被转换为相同的基类型,然后 然后 实际测试发生。 MDN has a list of rules for the conversion - 直接从它
1 == true
使用数字和布尔值,因此,两者的基本类型都是 number。 JavaScript 环境将通过调用 ToNumber(booleanValue)
来解决这个问题,所以这里是相等性测试实际测试的内容
var convertedBooleanOperand = Number(true);
console.log("convertedBooleanOperand", convertedBooleanOperand);
实际上,2 == true
转换为 2 == 1
即 false
。
0 为假,并不意味着其他数字为真..
简单的例子。如果你在条件中使用 === 那么你会看到所有数字都是 false。
(0 == true) // false
(1 == true) // true
它工作正常。但是,在下面的示例中,我没有在条件中使用 not 运算符 (!)。因此,如果条件为真,则应打印 true 否则为 false。
它仍然会给你相反的结果。
if(0){console.log("true")}else{console.log("false")} // false
if(1){console.log("true")}else{console.log("false")} // true
if(15){console.log("true")}else{console.log("false")} // true
现在,如果您将数字转换为布尔值,那么它会给出您所想的结果。
Boolean(0) == true // false
Boolean(1) == true // true
Boolean(2) == true // true
Boolean(othernumber) == true // true
谢谢
基于这些规则:
错误:
- 错误
- 0(零)
- '' 或 ""(空字符串)
- 无
- 未定义
- NaN(例如 1/0 的结果)
真实:其他一切
我找不到正确的解释,说明为什么在接下来的测试中,只有数字 1 的计算结果为 "true"
0 == true ("false")
1 == true ("true")
2 == true ("false")
othernumber == true ("false")
"truthy" 和 "falsy" 规则仅在值本身用作测试时适用,例如:
var str = "";
if (str) {
// It's truthy
} else {
// It's falsy
}
==
有自己的一套不同的规则来确定其操作数的松散相等性,这些规则在规范的 Abstract Equality Comparison algorithm:
- If Type(x) is the same as Type(y), then
- Return the result of performing Strict Equality Comparison x === y.
- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
- If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
- If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
- If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
- If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
- Return false.
有关其中列出的各种抽象操作的完整详细信息,请参阅规范,尽管名称几乎说明了它们的作用。 (如果你查看规范,你会在不同的地方看到 ToNumber
之前的 !
;我已经在上面删除了它。它是 not 逻辑 NOT运算符,它是 a spec notation related to "abrupt completions.")
让我们按照您的 2 == true
示例进行操作:
- 类型不一样,继续
- x 不为空,所以继续
- x 不是未定义的,所以继续
- Type(x) 确实是 Number,但 Type(y) 不是 String,所以继续
- Type(x) 不是字符串,所以继续
- Type(x) 不是布尔值,所以继续
- Type(y) 是布尔值,所以 return x == ToNumber(y) 的结果
- ToNumber(true) 为 1,由于 2 == 1 为假,结果为假
但请注意,您的 1 == true
示例的第 7 步有所不同:
- Type(y) 是布尔值,所以 return x == ToNumber(y) 的结果
- ToNumber(true) 为 1,由于 1 == 1 为真,所以结果为真
使用 ==
不同于 if(something)
此测试将为您提供预期结果:
function truthyFalsyTest()
{
if(0)
{
console.log("true");
}
else
{
console.log("false");
}
if(1)
{
console.log("true");
}
else
{
console.log("false");
}
if(2)
{
console.log("true");
}
else
{
console.log("false");
}
if(2222)
{
console.log("true");
}
else
{
console.log("false");
}
}
truthyFalsyTest();
这是一个非常有趣的原因,根据 this book,当您将任何东西与布尔值进行比较时,例如在 x == y
中,它遵循以下模式:
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
因此,当您比较 1 == true
时,它实际上在做 1 == ToNumber(true)
,然后变成 1 == 1
,但是当您比较 2 == true
时,它变成 2 == 1
这当然是错误的。
这和书中给出的其他一些原因建议不要将事物与布尔值进行比较。
当您与布尔值进行比较时,值本身是真值还是假值并不重要,因为它永远不会变成布尔值,但布尔值会被强制转换为可以与之比较的类型==
运算符的另一端。
希望您对这个回答满意。
在JavaScript中(==)是一个相等运算符运算符,在这里完成类型转换。更严格的(===)恒等运算符不会做类型转换。
例如,即使数字不是布尔值,如果使用 (==) 运算符,您也可以在需要布尔值的地方使用数值。
但是,如果您执行更严格的 (===) 运算符,您会看到“1 === true”的计算结果为 false。
Based on these rules: The issue here is that
==
does NOT operate by these rules. Whether something is truthy or not and how it behaves during equality tests are two different things. For the record a more correct test for truthyness would be
if (value)
return true;
else
return false;
甚至更短 - 直接转换 Boolean(value)
(以及隐式转换 !!value
.
但是,在相等性测试期间,==
的两边将被转换为相同的基类型,然后 然后 实际测试发生。 MDN has a list of rules for the conversion - 直接从它
1 == true
使用数字和布尔值,因此,两者的基本类型都是 number。 JavaScript 环境将通过调用 ToNumber(booleanValue)
来解决这个问题,所以这里是相等性测试实际测试的内容
var convertedBooleanOperand = Number(true);
console.log("convertedBooleanOperand", convertedBooleanOperand);
实际上,2 == true
转换为 2 == 1
即 false
。
0 为假,并不意味着其他数字为真.. 简单的例子。如果你在条件中使用 === 那么你会看到所有数字都是 false。
(0 == true) // false
(1 == true) // true
它工作正常。但是,在下面的示例中,我没有在条件中使用 not 运算符 (!)。因此,如果条件为真,则应打印 true 否则为 false。 它仍然会给你相反的结果。
if(0){console.log("true")}else{console.log("false")} // false
if(1){console.log("true")}else{console.log("false")} // true
if(15){console.log("true")}else{console.log("false")} // true
现在,如果您将数字转换为布尔值,那么它会给出您所想的结果。
Boolean(0) == true // false
Boolean(1) == true // true
Boolean(2) == true // true
Boolean(othernumber) == true // true
谢谢