How to check if a variable is not null?

前端 未结 9 1061
太阳男子
太阳男子 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: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.

提交回复
热议问题