How can I check for null values in 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;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
JavaScript is very flexible with regards to checking for "null" values. I’m guessing you’re actually looking for empty strings, in which case this simpler code will work:
Which will check for empty strings (
""),null,undefined,falseand the numbers0andNaN.Please note that if you are specifically checking for numbers, it is a common mistake to miss
0with this method, andnum !== 0is preferred (ornum !== -1or~num(hacky code that also checks against-1)) for functions that return-1, e.g.indexOf).