是否有过一个理由,写"若(myBoolean ==真)"在JavaScript的条件? [英] Is there ever a reason to write "if (myBoolean == true)" in a JavaScript conditional?

查看:87
本文介绍了是否有过一个理由,写"若(myBoolean ==真)"在JavaScript的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多初级程序员写出来的东西像如果(myBoolean == true)而,而不是如果(myBoolean)作为他们尚未掌握该条件语句不需要包含一比较,仅仅一个布尔值。当我提到这在twitter上有人向我建议在JavaScript中有可能是一个很好的理由这样做。在那里?我们都知道的JavaScript类型是陌生的,但谷歌得出这个特定点没有答案。

Many beginner programmers write things like if (myBoolean == true) as opposed to if (myBoolean) as they haven't yet grasped that conditionals don't need to contain a comparison, merely a boolean. When I noted this on twitter it was suggested to me that in JavaScript there might be a good reason to do this. Is there? We all know JavaScript types are strange, but Google yields no answers on this specific point.

更新:似乎没有区别,但有将与一个区别,如果(myBoolean ===真)。因此,要澄清的问题 - 这将是在JavaScript中,如果(myBoolean ===真)或者(myBoolean)的最佳实践。在什么真实的场景中,你会成为检查 ===真正来的东西,你不知道的是一个布尔值?请在此跟进的问题回答:<一href=\"http://stackoverflow.com/questions/13250664/why-is-it-good-practice-to-use-if-myboolean-true-in-javascript\">Why它是很好的做法,使用if(myBoolean ===真)在JavaScript中?

Update: It seems there is no difference, however there would be a difference with if (myBoolean === true). So to clarify the question - what would be the best practice in JavaScript if (myBoolean === true) or if (myBoolean). In what real-world scenario would you be checking === true to something that you don't know is a boolean? Please answer in this follow-up question: Why is it good practice to use if (myBoolean === true) in JavaScript?

推荐答案

没有。

由于您使用non-strict平等运营商,也绝对没有什么区别:

Since you're using the non-strict equality operator, there is absolutely no difference between:

if (myBoolean == true)

if (myBoolean)

有将是一个区别,如果你使用全等运算符 === myBoolean 本来就不是一个布尔值,虽然。

There would be a difference if you were using the strict equality operator === and myBoolean was not actually a boolean, though.

这篇关于是否有过一个理由,写&QUOT;若(myBoolean ==真)&QUOT;在JavaScript的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆