I have a piece of code that tests for the existence of a variable, using an if statement like the example below. I need to do one thing if the var is set, a different thing if its not. In a certain test case, the var needed to be set to 0, but that is the same is having an unset var in JS, apparently:
我有一段代码来测试变量的存在性,使用如下面的示例所示的if语句。如果设置了var,我需要做一件事,如果没有,我需要做另一件事。在某个测试用例中,var需要设置为0,但显然,在JS中有一个未设置的var:
var toMatch;
toMatch = 0;
if (!toMatch) {
document.write("no");
} else {
document.write(toMatch);
}
// html is "no"
jsFiddle
So my problem is, how do I test for a var if its value is legitimately zero. I should point out, that in my function possible values to be passed are 0-40+.
我的问题是,如果var的值合理地为零,我该如何测试它呢?我应该指出,在我的函数中,可能要传递的值是0-40+。
In the past I've used a work around like setting the initial value to a number high enough that it is not likely to be passed to the function but that seems hackey to me. Is there a better way to do it?
在过去,我使用过这样的工作,比如将初始值设置为足够高的值,这样它就不太可能被传递给函数,但对我来说,这似乎是hackey。有更好的方法吗?
4 个解决方案
#1
16
var toMatch;
toMatch = 0;
if (toMatch === 0) { // or !== if you're checking for not zero
document.write("no");
} else {
document.write(toMatch);
}
toMatch === 0
will check for zero.
番茄=== 0将检查是否为0。
toMatch === undefined
will check for undefined
toMatch === == undefined将检查是否为undefined
the triple equals are strict comparison operators for this sort of scenario. See this blessed question: Difference between == and === in JavaScript
对于这种情况,三重等号是严格的比较运算符。请参见JavaScript中==和===之间的差异
#2
2
You can see if a name is undefined with:
您可以看到名称是否未定义为:
if (typeof bad_name === "undefined") {
#3
2
Instead of
而不是
if (toMatch)
use
使用
if (toMatch == null)
#4
0
Javascript is a bit funny when it comes to values and boolean checks. I suggest reading about Truthy and Falsey in Javascript.
当涉及到值和布尔值检查时,Javascript有点滑稽。我建议用Javascript阅读Truthy和Falsey。
You should use the identity inequality operator !==
:
您应该使用identity不等式操作符!=:
var toMatch;
toMatch = 0;
if (toMatch !== 0) {
document.write("no");
} else {
document.write(toMatch);
}
It is also worth understanding the differences between ==
and ===
.
==和==之间的差异也值得理解。
#1
16
var toMatch;
toMatch = 0;
if (toMatch === 0) { // or !== if you're checking for not zero
document.write("no");
} else {
document.write(toMatch);
}
toMatch === 0
will check for zero.
番茄=== 0将检查是否为0。
toMatch === undefined
will check for undefined
toMatch === == undefined将检查是否为undefined
the triple equals are strict comparison operators for this sort of scenario. See this blessed question: Difference between == and === in JavaScript
对于这种情况,三重等号是严格的比较运算符。请参见JavaScript中==和===之间的差异
#2
2
You can see if a name is undefined with:
您可以看到名称是否未定义为:
if (typeof bad_name === "undefined") {
#3
2
Instead of
而不是
if (toMatch)
use
使用
if (toMatch == null)
#4
0
Javascript is a bit funny when it comes to values and boolean checks. I suggest reading about Truthy and Falsey in Javascript.
当涉及到值和布尔值检查时,Javascript有点滑稽。我建议用Javascript阅读Truthy和Falsey。
You should use the identity inequality operator !==
:
您应该使用identity不等式操作符!=:
var toMatch;
toMatch = 0;
if (toMatch !== 0) {
document.write("no");
} else {
document.write(toMatch);
}
It is also worth understanding the differences between ==
and ===
.
==和==之间的差异也值得理解。