javascript 在 JS 中 if (condition) 表示 == true 或 === true
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17883717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In JS if (condition) means == true or === true
提问by amandanovaes
I know the difference between ==
and ===
however I always believed that if (condition)
condition
was supposed to be evaluated against true
using strict equality (===
) and not type-coercing equality (==
).
我知道两者之间的区别==
,===
但是我始终认为if (condition)
condition
应该根据true
使用严格相等 ( ===
) 而不是类型强制相等 ( ==
)进行评估。
See an example:
看一个例子:
if (1) {
console.log("1");
}
if (1 == true) {
console.log("2");
}
if (1 === true) {
console.log("3");
}
It returns:
它返回:
::1
::2
I know 1 is not strictly equal to true
, because the type is different, but when I do if (condition)
according to W3C it should be the strict equality test (===
) that is run not the type-coercing equality of ==
.
我知道 1 并不严格等于true
,因为类型不同,但是当我if (condition)
根据 W3C 进行操作时,应该运行的是严格相等测试 ( ===
) 而不是==
.
So why is it logging 1?
那么为什么它会记录 1?
回答by Serlite
The if
statement uses condition == true
. It's given in the ECMAScript Language Specification, here: http://www.ecma-international.org/ecma-262/5.1/#sec-12.5
该if
语句使用condition == true
. 它在 ECMAScript 语言规范中给出,这里:http: //www.ecma-international.org/ecma-262/5.1/#sec-12.5
Note the usage of the ToBoolean()
in Step 2. This converts the given argument to a Boolean value, meaning that yes, type coercion does occur for the condition of an if
statement.
注意ToBoolean()
步骤 2 中的用法。这会将给定的参数转换为布尔值,这意味着是的,对于if
语句的条件确实会发生类型强制。
回答by deceze
Javascript a.k.a. ECMAScript is not governed by the W3C but by ECMA. You can read the specification here. What you're interested in is thissection, which specifies how expressions in if
statements are handled. The runtime should call toBoolean
on the expression value; meaning any type is going to be converted to a boolean.
Javascript aka ECMAScript 不是由 W3C 管理,而是由 ECMA 管理。您可以在此处阅读规范。您感兴趣的是这一节,它指定如何if
处理语句中的表达式。运行时应该调用toBoolean
表达式值;意味着任何类型都将被转换为布尔值。
Therefore it behaves like ==
.
因此它的行为就像==
.
回答by Praveen
Because ===
is strict comparison operator.
因为===
是严格的比较运算符。
if (1 === true) { //they are of a different type
Have you tried this
你有没有试过这个
if (0) { // This fails
console.log("1");
}
because 0 = off, negative, no, false
universally. check this Why is 0 false?
因为0 = off, negative, no, false
普遍。检查这个为什么 0 是假的?
whereas whereas when you use ===
而当你使用 ===
if( 1 === true) //It looks for type 1 as number and true as boolean
ES5 spec defines the following algorithm:
回答by sasha.sochka
It is equivalent to condition == true
, which is specified by ECMAScript Language Specification
它等价于condition == true
,由 ECMAScript 语言规范指定
回答by Vinit Prajapati
The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.
3 个等号表示“没有类型强制的平等”。使用三重等号时,值的类型也必须相等。
回答by Elias Van Ootegem
As you're getting this info from a book on CSS, the author is most likely referring to CSS rules like this:
当你从一本关于 CSS 的书中获得这些信息时,作者很可能指的是这样的 CSS 规则:
[if IE]body
{/* only for IE*/
[if IE 6] background: url('get_real.gif');
[if gt IE 6] background: url('nogood_browser.png');
}
[if Webkit]body
{/*Only webkit browsers get this bg*/
background: url('webkit_bg.png');
}
[if Gecko]body
{/*Gecko-based browsers get this*/
background: url('foxy.png');
}
Other than that - On JS:
除此之外 - 在 JS 上:
My guess would be you got the idea from the place all mis-information comes from. Please, for your sake and mine, don't use w3schools.
我的猜测是你从所有错误信息的来源那里得到了这个想法。为了你我的缘故,请不要使用 w3schools。
in case of an if (condition)
, where condition
should read expression
to be correct, it should be truthy, if the expression consists of a single value. In case you are comparing 2 expressions, the conditional expression evaluates to true
or false
, based on how you compare the two operands
在的情况下if (condition)
,在这里condition
应该读expression
是正确的,它应该是truthy,如果表达式由一个单一的值。如果您正在比较 2 个表达式,则条件表达式的计算结果为true
or false
,具体取决于您比较两个操作数的方式
Single operand:
单操作数:
if (1)//truthy
if (undefined) //falsy
This is because Boolean(1) === true
and Boolean(undefined) === false
Once you introduce a second operand:
这是因为,Boolean(1) === true
和Boolean(undefined) === false
一旦引入第二个操作数:
if ('0' == 0)//=== true
if ('0' === 0)// === false
That's because ===
is a type andvalue check, which you obviously know already. Just so there's no confusion:
那是因为===
是类型和值检查,您显然已经知道了。只是为了没有混淆:
If you want to be sure you have the correct information: check the ECMAScript language specs, not some third party resource:here's the link
如果你想确保你有正确的信息:检查 ECMAScript 语言规范,而不是一些第三方资源:这里是链接
回答by Abhitalks
Truthy: Something which evaluates to TRUE. Falsey: Something which evaluates to FALSE.
真实:评估为 TRUE 的东西。Falsey:评估结果为 FALSE 的东西。
1 is truthy, 0 is falsey.
1为真,0为假。
An object of any kind (including functions, arrays) is always truthy.
任何类型的对象(包括函数、数组)都是真实的。
These are all falsy: undefined, null, NaN, 0, "" (empty string), and false.
这些都是假的:undefined、null、NaN、0、""(空字符串)和假。
Hence, in your first condition if (1)...
logs 1 because 1 is truthy.
因此,在您的第一个条件中if (1)...
记录 1,因为 1 是真实的。
=== specifically tests for type equality, which is different from testing truthy.
=== 专门测试类型相等,这与测试truthy 不同。
回答by Fiona - myaccessible.website
回答by Mateen
Note:there's a simple way to convert an expression to Boolean and it is using !! operator. For Example
!!1 is true
!!{} is true
!!"" is false and !!"asdf" is true
so i always feel comfortable converting expressions to Boolean and then letting them into an if comparison test
注意:有一种将表达式转换为布尔值的简单方法,它使用 !! 操作员。例如
!!1 为真
!!{} 为真
!!"" 为假而 !!"asdf" 为真,
所以我总是觉得将表达式转换为布尔值,然后让它们进入 if 比较测试
if (1) { // !!1 is true and anything non-zero is true even -ve numbers just like c language
console.log("1");
}
if (1 == true) { //!! 1 is true and we are not checking type only two equals (==)
console.log("2");
}
if (1 === true) { //!! 1 is true but the type is number which is obviously not a Boolean type
console.log("3");
}