Javascript 如何检查变量是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4361585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:05:17  来源:igfitidea点击:

How to check if a variable is not null?

javascriptcomparisonnull-check

提问by nickb

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.

我知道以下是 JavaScript 中检查变量是否为 not 的两种方法null,但我很困惑哪个是使用的最佳实践。

Should I do:

我应该这样做:

if (myVar) {...}

or

或者

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

回答by Tim Down

They are not equivalent. The first will execute the block following the ifstatement if myVaris truthy(i.e. evaluates to truein a conditional), while the second will execute the block if myVaris any value other than null.

它们不是等价的。第一个将以下所述执行块if语句如果myVartruthy(即计算结果为true在条件),而如果第二将执行块myVar比其它任何值null

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

JavaScript 中唯一不真实的值如下(又名虚假值):

  • null
  • undefined
  • 0
  • ""(the empty string)
  • false
  • NaN
  • null
  • undefined
  • 0
  • ""(空字符串)
  • false
  • NaN

回答by Tolgahan Albayrak

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

以下是测试变量是否为 NULL 的方法:

if (myVar !== 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 0or NaNor anything else..

如果myVar的不为空..如果myVar的是不明确的或虚假的或将要执行的块将被执行0NaN或其他任何东西..

回答by Jonathon Bolster

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

阅读这篇文章: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:

它对 JavaScript 有一些很好的提示,但它确实提到的一件事是你应该检查 null,如:

if(myvar) { }

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

它还提到了您可能没有意识到的被认为是“虚假”的东西。

回答by Kamil Kie?czewski

  • code inside your if(myVar) { code }will be NOT executed only when myVaris equal to: false, 0, "", null, undefined, NaNor you never defined variable myVar(then additionally code stop execution and throw exception).
  • code inside your if(myVar !== null) {code}will be NOT executed only when myVaris equal to nullor you never defined it (throws exception).
  • if(myVar) { code }仅当myVar等于时才不会执行您内部的代码:false, 0, "", null, undefined, NaN或者您从未定义变量myVar(然后另外代码停止执行并抛出异常)。
  • if(myVar !== null) {code}仅当myVar等于null或您从未定义它(抛出异常)时,才不会执行您内部的代码。

Here you have all (src)

在这里你有所有(src

if

如果

enter image description here

在此处输入图片说明

==(its negation !=)

==(它的否定!=

enter image description here

在此处输入图片说明

===(its negation !==)

===(它的否定!==

enter image description here

在此处输入图片说明

回答by Flat Cat

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:

我做了一个 ajax 调用,并以字符串格式将数据返回为 null。我不得不像这样检查它:

if(value != 'null'){}

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

因此,null 是一个读取“null”而不是真正为 null 的字符串。

EDIT:It should be understood that I'm not selling this as the way it shouldbe 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.

编辑:应该理解,我不是按照应该的方式出售它。我有一个场景,这是唯一可以完成的方法。我不知道为什么......也许编写后端的人错误地呈现了数据,但无论如何,这就是现实生活。令人沮丧的是,看到有人知道这不太正确,然后又被实际上有帮助的人投票赞成,这令人沮丧。

回答by Sanjay Kumaar

if myVaris null then if block not execute other-wise it will execute.

如果myVar为空,则如果块不执行,否则它将执行。

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

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

回答by OpenUser

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

有时,如果甚至没有定义,最好做好准备。为此,我使用了typeof

if(typeof(variable) !== "undefined") {
    //it exist
    if(variable !== null) {
        //and is not null
    }
    else {
        //but is null
    }
}
else {
    //it doesn't
}

回答by THE DOCTOR

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 myVaris true and executes the code inside of the {...}

第一个示例确定myVar 中的值是否为真并执行{...} 中的代码

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

第二个示例评估myVar是否不等于 null,如果该情况为真,它将在{...}内执行您的代码

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

我建议查看条件语句以获取更多技术。一旦您熟悉了它们,您就可以决定何时需要它们。