如何在JavaScript中检查空值?我写了下面的代码,但没用。
if (pass == null || cpass == null || email == null || cemail == null || user == null) {
alert("fill all columns");
return false;
}
如何在JavaScript程序中查找错误?
如何在JavaScript中检查空值?我写了下面的代码,但没用。
if (pass == null || cpass == null || email == null || cemail == null || user == null) {
alert("fill all columns");
return false;
}
如何在JavaScript程序中查找错误?
当前回答
与操作员进行可选检查怎么样?
例如:
// check mother for null or undefined and
// then if mother exist check her children also
// this 100% sure it support and valid in JS today.
// Apart of that C# have almost the same operator using the same way
if (mother?.children) {
}
else {
// it is null, undefined, etc...
}
其他回答
我做了一个非常简单的功能,效果很好:
function safeOrZero(route) {
try {
Function(`return (${route})`)();
} catch (error) {
return 0;
}
return Function(`return (${route})`)();
}
路线是任何一条价值链都会爆炸。我将它用于jQuery/cacherio和对象等。
示例1:一个简单的对象,例如this const testObj={items:[{val:'haya'},{val:null},{val:'hum!'}];};。
但它可能是一个非常大的物体,我们甚至没有制造过。所以我通过了:
let value1 = testobj.items[2].val; // "hum!"
let value2 = testobj.items[3].val; // Uncaught TypeError: Cannot read property 'val' of undefined
let svalue1 = safeOrZero(`testobj.items[2].val`) // "hum!"
let svalue2 = safeOrZero(`testobj.items[3].val`) // 0
当然,如果您愿意,可以使用null或“无值”。。。任何适合您需要的。
通常,如果找不到DOM查询或jQuery选择器,可能会抛出错误。但使用类似于:
const bookLink = safeOrZero($('span.guidebook > a')[0].href);
if(bookLink){
[...]
}
要检查空值,请使用以下命令:
if (variable === null)
此测试仅通过null,不通过“”、undefined、false、0或NaN。
此外,我还为每个“类假”值提供了绝对检查(对于!变量返回true)。
注意,对于某些绝对检查,您需要使用绝对等于:==和typeof。
我在这里创建了一个JSFiddle,以显示所有单独测试的工作情况
以下是每次检查的输出:
Null Test:
if (variable === null)
- variable = ""; (false) typeof variable = string
- variable = null; (true) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Empty String Test:
if (variable === '')
- variable = ''; (true) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Undefined Test:
if (typeof variable == "undefined")
-- or --
if (variable === undefined)
- variable = ''; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (true) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
False Test:
if (variable === false)
- variable = ''; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (true) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Zero Test:
if (variable === 0)
- variable = ''; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (true) typeof variable = number
- variable = NaN; (false) typeof variable = number
NaN Test:
if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)
-- or --
if (isNaN(variable))
- variable = ''; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (true) typeof variable = number
正如你所看到的,测试NaN有点困难;
您可以检查某些值是否为空,如下所示
[pass,cpass,email,cemail,user].some(x=> x===null)
let pass=1;设cpass=2;让电子邮件=3;let cemail=空;让用户=5;如果([pass,cpass,email,cemail,user].some(x=>x==null)){警报(“填充所有列”);//return false;}
奖金:为什么==比==更清楚(来源)
a==b
a===b
乍一看,这似乎是一个简单的覆盖和严格之间的权衡。
==包含多个值,可以用更少的代码处理更多的场景。==是最严格的,这使得它可以预测。
可预测性总是获胜的,这似乎是一个万能的解决方案。
但这是错误的。尽管==是可预测的,但它并不总是产生可预测的代码,因为它忽略了场景。
const options = { };
if (options.callback !== null) {
options.callback(); // error --> callback is undefined.
}
通常,==为空检查执行更可预测的工作:
一般来说,null和undefined都意味着同一件事:“缺少了什么”。为了实现可预测性,您需要检查这两个值。然后==null做得很好,因为它正好覆盖了这两个值。(即==null等同于==null&&==未定义)在特殊情况下,您确实需要明确区分null和undefined。在这些情况下,最好使用严格的==未定义或==null。(例如,缺失/忽略/跳过和空/清除/删除之间的区别。)但这很少见。
这不仅是罕见的,也是需要避免的。不能在传统数据库中存储undefined。由于互操作性的原因,在API设计中也不应该依赖未定义的值。但即使你根本不做区分,你也不能假设未定义不会发生。我们周围的人都间接地采取了泛化空/未定义的行为(这就是为什么像这样的问题被封闭为“固执己见”的原因)。
所以,回到你的问题。使用==null没有错。它正是它应该做的。
// FIX 1 --> yes === is very explicit
const options = { };
if (options.callback !== null &&
options.callback !== undefined) {
options.callback();
}
// FIX 2 --> but == covers both
const options = { };
if (options.callback != null) {
options.callback();
}
// FIX 3 --> optional chaining also covers both.
const options = { };
options.callback?.();
在JavaScript中,没有字符串等于null。
当pass为空字符串时,您可能希望pass==null为真,因为您知道松散的相等运算符==执行某些类型的强制。
例如,此表达式为true:
'' == 0
相反,严格相等运算符==表示这是错误的:
'' === 0
假定“”和0大致相等,您可以合理地推测“”和null大致相等。然而,事实并非如此。
此表达式为false:
'' == null
将任何字符串与null进行比较的结果为false。因此,pass==null和所有其他测试始终为false,用户永远不会收到警报。
要修复代码,请将每个值与空字符串进行比较:
pass === ''
如果您确定pass是一个字符串,pass==“”也会起作用,因为只有空字符串与空字符串大致相等。另一方面,一些专家表示,在JavaScript中始终使用严格相等是一种好的做法,除非您特别想执行松散相等运算符执行的类型强制。
如果您想知道哪些值对大致相等,请参阅Mozilla文章中关于此主题的“Sameness比较”表。