How to check 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; }
How to find errors in JavaScript programs?
#1 building
To check empty specifically, you do this:
if (variable === null)
This test will pass only null, not '', undefined, false, 0 or NaN.
In addition, I provide an absolute check for each "fake" value (a! variable that returns true for! variable).
Note that for some absolute checks, you will need to use absolute equals: = = and typeof.
I created a JSFiddle here to show all the individual tests
This is the output of each check:
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
As you can see, it is more difficult to test NaN;
#2 building
This is a comment on WebWanderer's solution for checking NaN (I don't have enough representatives to make a formal comment). Solution read as
if(!parseInt(variable) && variable != 0 && typeof variable === "number")
But this will fail for rational numbers that are rounded to 0 (for example, variable = 0.1). Better tests are:
if(isNaN(variable) && typeof variable === "number")
#3 building
To check undefined and null in javascript, simply write the following code:
if (!var) { console.log("var IS null or undefined"); } else { console.log("var is NOT null or undefined"); }
#4 building
In JavaScript, no string is equal to null.
Perhaps you want pass == null to true when pass is an empty string because you know that the loose equal sign operator = = performs some type of cast.
For example, this expression is true:
'' == 0
Instead, the strict equality operator = = = indicates that this is wrong:
'' === 0
Assuming that '' and 0 are approximately equal, it is reasonable to assume that '' and null are approximately equal. However, this is not the case.
This expression is incorrect:
'' == null
The result of comparing any string with null is false. Therefore, pass == null and all other tests are always false, and the user will never receive an alert.
To fix the code, compare each value with an empty string:
pass === ''
If you determine that pass is a string, pass = = '' also works, because only one empty string is roughly equal to that empty string. On the other hand, some experts say it's a good habit to always use strict equality in JavaScript unless you specifically want to perform type coercion performed by the loose equality operator.
If you want to know which pairs of values are roughly equal, see About this topic Of Mozilla articles Similarity comparison table in.
#5 building
When a variable is declared but no value is assigned, the AFAIK in JAVASCRIPT is of type undefined. So we can check variables, even if it's an object that holds some instances instead of values.
Create a helper method to check for invalid return true and use it in your API.
Helper function to check if the variable is empty:
function isEmpty(item){ if(item){ return false; }else{ return true; } }
Try to catch an exception API call:
try { var pass, cpass, email, cemail, user; // only declared but contains nothing. // parametrs checking if(isEmpty(pass) || isEmpty(cpass) || isEmpty(email) || isEmpty(cemail) || isEmpty(user)){ console.log("One or More of these parameter contains no vlaue. [pass] and-or [cpass] and-or [email] and-or [cemail] and-or [user]"); }else{ // do stuff } } catch (e) { if (e instanceof ReferenceError) { console.log(e.message); // debugging purpose return true; } else { console.log(e.message); // debugging purpose return true; } }
Some test cases:
var item = ""; // isEmpty? true var item = " "; // isEmpty? false var item; // isEmpty? true var item = 0; // isEmpty? true var item = 1; // isEmpty? false var item = "AAAAA"; // isEmpty? false var item = NaN; // isEmpty? true var item = null; // isEmpty? true var item = undefined; // isEmpty? true console.log("isEmpty? "+isEmpty(item));