admin管理员组文章数量:1313273
How to right this syntax correctly:
if (tipoTropaPrioritaria[m] || troopsCount[m] || availableTroops[m]) == ("null" || "undefined") {
...
}
(to check if any of the first 3 variables is null or undefined)
How to right this syntax correctly:
if (tipoTropaPrioritaria[m] || troopsCount[m] || availableTroops[m]) == ("null" || "undefined") {
...
}
(to check if any of the first 3 variables is null or undefined)
Share Improve this question asked May 24, 2010 at 21:07 FernandoSBSFernandoSBS 6555 gold badges12 silver badges23 bronze badges 3-
7
Do you mean the values
null
andundefined
or the string values"null"
and"undefined"
? – Gumbo Commented May 24, 2010 at 21:10 - not string values, the "internal javascript" null/undefined value. – FernandoSBS Commented May 24, 2010 at 21:33
- Remember to accept a solution when you are satisfied. – Sean Kinsey Commented May 25, 2010 at 14:22
7 Answers
Reset to default 3You could define a small helper function that does the check and then use it on all the values:
function notset(v) {
return (v === undefined) || (v === null);
}
if (notset(tipoTropaPrioritaria[m]) || notset(troopsCount[m]) ||
notset(availableTroops[m])) {
...
}
Use:
if (tipoTropaPrioritaria[m] == null || troopsCount[m] == null || availableTroops[m] == null ||
tipoTropaPrioritaria[m] == undefined || troopsCount[m] == undefined || availableTroops[m] == undefined) {
// ...
}
EDIT: It's probably better to use the ===
(threequals) operator in this case.
if (tipoTropaPrioritaria[m] === null || troopsCount[m] === null || availableTroops[m] === null ||
tipoTropaPrioritaria[m] === undefined || troopsCount[m] === undefined || availableTroops[m] === undefined) {
// ...
}
or:
if (null in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0} || undefined in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0}) {
The way to do is:
if ((tipoTropaPrioritaria[m] == null || tipoTropaPrioritaria[m] == undefined)
|| (troopsCount[m] == null || troopsCount[m] == undefined) ||
(availableTroops[m] == null || availableTroops[m] == undefined)) {
...
}
If you do this a lot you can create a helper function
function isNullOrUndef(args) {
for (var i =0; i < arguments.length; i++) {
if (arguments[i] == null || typeof arguments[i] === "undefined") {
return true
}
}
return false
}
if (isNullOrUndef(tipoTropaPrioritaria[m], troopsCount[m], availableTroops[m]))
...
This is the best way to do what you want
if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m]) {
...
}
The !
operator coerces the result into a boolean that can be tested for (null and undefined bees false), and with the !
in front false
is negated into true
.
The other way to do it is to test each expression against null
and undefined
.
function isNullOrUndefined(val) {
return (val === null || typeof val == "undefined");
}
if (isNullOrUndefined(a) || isNullOrUndefined(b) ... ) {
And so you know it, the correct way to test for undefined is
if (typeof foo === "undefined") {...
if (tipoTropaPrioritaria[m] && troopsCount[m] && availableTroops[m]) { }
else { /* At least ones is undefined/null OR FALSE */ }
if (tipoTropaPrioritaria[m] == null || troopsCount[m] == null || availableTroops[m] == null)
{ /* One is null. */ }
if you want to check if they are null or undefined you can write
if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m])
both null and undefined
are falsely values. Then this will only be true if none of this are null or undefined.
Please consider that there are other falsely values as well:
- 0
- empty string
- NaN
本文标签: operatorsJavascript how to compare multiple variables valueStack Overflow
版权声明:本文标题:operators - Javascript ||, how to compare multiple variables value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741929802a2405508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论