function test(variable, check) {
var check = check || ???;
if (variable == check) {
//do stuff
}
}
我希望此函数检查变量是否是 if(variable) return true;
将返回 true 的任何值,例如任何大于 0 的数字。我想保留这种简单的格式,并在 check
可能是特定值时使用 ==
运算符,所以我可以将其默认为什么以实现这个?
我可以用这段代码实现同样的目的:
function test(variable, check) {
if(check) {
//check specific value
if (variable == check) {
//do stuff
}
} else {
//check isn't set, so accept any truthy values (like 5 or 'string')
if(variable) {
//do stuff
}
}
}
最佳答案
聊了一会儿,这里是答案:
if (check ? check == variable : variable) {
// do something
}
其实很明显... =D
关于javascript - 使用 == 运算符检查变量是否为 0/false/null 等以外的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21796041/