How to check if a variable is not null?

前端 未结 9 1011
太阳男子
太阳男子 2020-11-28 17:47

I know that below are the two ways in JavaScript to check whether a variable is not null, but I’m confused which is the best practice to use.

Should I d

相关标签:
9条回答
  • 2020-11-28 18:02

    The two conditional statements you list here are not better than one another. Your usage depends on the situation. You have a typo by the way in the 2nd example. There should be only one equals sign after the exclamation mark.

    The 1st example determines if the value in myVar is true and executes the code inside of the {...}

    The 2nd example evaluates if myVar does not equal null and if that case is true it will execute your code inside of the {...}

    I suggest taking a look into conditional statements for more techniques. Once you are familiar with them, you can decide when you need them.

    0 讨论(0)
  • 2020-11-28 18:05

    They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. evaluates to true in a conditional), while the second will execute the block if myVar is any value other than null.

    The only values that are not truthy in JavaScript are the following (a.k.a. falsy values):

    • null
    • undefined
    • 0
    • "" (the empty string)
    • false
    • NaN
    0 讨论(0)
  • 2020-11-28 18:05

    There is another possible scenario I have just come across.

    I did an ajax call and got data back as null, in a string format. I had to check it like this:

    if(value != 'null'){}
    

    So, null was a string which read "null" rather than really being null.

    EDIT: It should be understood that I'm not selling this as the way it should be done. I had a scenario where this was the only way it could be done. I'm not sure why... perhaps the guy who wrote the back-end was presenting the data incorrectly, but regardless, this is real life. It's frustrating to see this down-voted by someone who understands that it's not quite right, and then up-voted by someone it actually helps.

    0 讨论(0)
  • 2020-11-28 18:07

    Here is how you can test if a variable is not NULL:

    if (myVar !== null) {...}

    the block will be executed if myVar is not null.. it will be executed if myVar is undefined or false or 0 or NaN or anything else..

    0 讨论(0)
  • 2020-11-28 18:12

    Have a read at this post: http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-2/

    It has some nice tips for JavaScript in general but one thing it does mention is that you should check for null like:

    if(myvar) { }
    

    It also mentions what's considered 'falsey' that you might not realise.

    0 讨论(0)
  • 2020-11-28 18:15

    Sometimes if it was not even defined is better to be prepared. For this I used typeof

    if(typeof(variable) !== "undefined") {
        //it exist
        if(variable !== null) {
            //and is not null
        }
        else {
            //but is null
        }
    }
    else {
        //it doesn't
    }
    
    0 讨论(0)
提交回复
热议问题