How do we check for null values in JavaScript?

How do we check for null values in JavaScript?

Asked on October 22, 2018 in Javascript.
Add Comment


  • 5 Answer(s)

    You can use:

    if (pass == null || cpass == null || email == null || cemail == null || user == null) {
        alert("fill all columns");
        return false;
    }
    

    The if made to check all the null values of possibilities in java script and above code is used to check it.

     

    Answered on October 22, 2018.
    Add Comment

    Javascript is very flexible with regards to checking for “null” values. Actually we are 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

    Please note that if we 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, e.g. indexOf)

    Answered on October 22, 2018.
    Add Comment

    To check for null SPECIFICALLY we would use this:

    if(variable === null && typeof variable === "object")
    

    …or more simply:

    if(variable === null)
    

    This test will ONLY pass for null and will not pass for “”, undefined, false, 0, or NaN.

    The rest of this is in response to inorganik’s comment, Yes, we can check each one individually.

    We need to implement use of the absolutely equals: === and typeof to be absolutely sure with wer checks.
    I’ve created a JSFiddle here to show all of the individual tests working

    Here is all of the output of the tests:

    Null Test:
    if(variable === null && typeof variable === "object")
    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 === "" && typeof variable === "string")
    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(variable === undefined && typeof 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 && typeof variable === "boolean")
    - 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 && typeof variable === "number")
    - 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(!parseFloat(variable) && variable != 0 && typeof variable === "number")
    - 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
    
    Answered on October 22, 2018.
    Add Comment

    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:

    if(!pass || !cpass || !email || !cemail || !user){

    Which will check for empty strings (""), null, undefined, false and the numbers 0 and NaN

    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, e.g. indexOf)

    Answered on February 13, 2019.
    Add Comment

    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:

    if(!pass || !cpass || !email || !cemail || !user){

    Which will check for empty strings (""), null, undefined, false and the numbers 0 and NaN

    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, e.g. indexOf)

    Answered on February 13, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.