How to check if a variable is not null?

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

提交回复
热议问题