Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

javascript - 如何检查JavaScript中的空值?(How do I check for null values in JavaScript?)

How can I check for null values in JavaScript?(如何在JavaScript中检查空值?)

I wrote the code below but it didn't work.(我在下面编写了代码,但是没有用。)
if (pass == null || cpass == null || email == null || cemail == null || user == null) {      

    alert("fill all columns");
    return false;  

}   

And how can I find errors in my JavaScript programs?(在JavaScript程序中如何找到错误?)

  ask by Mahdi_Nine translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Javascript is very flexible with regards to checking for "null" values.(Javascript在检查“空”值方面非常灵活。)

I'm guessing you're actually looking for empty strings, in which case this simpler code will work:(我猜您实际上是在寻找空字符串,在这种情况下,这种简单的代码将起作用:)
if(!pass || !cpass || !email || !cemail || !user){

Which will check for empty strings ( "" ), null , undefined , false and the numbers 0 and NaN(它将检查空字符串( "" ), nullundefinedfalse以及数字0NaN)

Please note that if you are specifically checking for numbers it is a common mistake to miss 0 with this method, and num !== 0 is preferred (or num !== -1 or ~num (hacky code that also checks against -1 )) for functions that return -1 , eg indexOf )(请注意,如果您专门检查数字,则使用此方法会遗漏0是一个常见错误,并且num !== 0是首选(或num !== -1~num (同样会检查-1 hacky代码) ))返回-1函数,例如indexOf ))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...