检查 JavaScript 对象是否为 null subtelty

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18421995/
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-10-27 11:50:57  来源:igfitidea点击:

Checking JavaScript object for null subtelty

javascript

提问by Peter Kellner

I believe I have found cases where I need to check for both undefined and null for a javascript object as follows:

我相信我已经找到了需要检查 javascript 对象的 undefined 和 null 的情况,如下所示:

if (x !== undefined && x != null && x.length > 0) {...}

However, in a recent upgrade of JetBrains tools, it tells me that this is sufficient

但是,在最近的 JetBrains 工具升级中,它告诉我这已经足够了

if (x != undefined && x.length > 0) {...}

My question is, I'm just wanting to insure that a string "x" has a length of non-zero and is not undefined or null (with the least amount tests).

我的问题是,我只是想确保字符串“x”的长度不为零,并且不是未定义的或空的(使用最少的测试)。

Thoughts?

想法?

回答by gdoron is supporting Monica

in javascript

在 JavaScript 中

undefined == null // true
undefined === null // false

so checking with ==for undefinedmakes the ==check for nullredundant.

所以检查==forundefined会使==检查变得null多余。

回答by Dennis

Try

尝试

if (x && x.length)

as undefined, nulland 0are all falsy values.

as undefinednull0都是假值。

EDIT: as you seem to know that xshould be a string, you can also just use if (x)as an empty string is also falsy.

编辑:正如你似乎知道x应该是 a string,你也可以只使用if (x)一个空字符串也是假的。

回答by Alaeddine

You can use _.isNullfrom Underscorea JavaScript library provides a whole mess of useful functional programming helpers.

你可以_.isNullUnderscore使用一个 JavaScript 库,它提供了一大堆有用的函数式编程助手。

_.isNull(object)

_.isNull(对象)

Returns true if the value of object is null.

如果 object 的值为 null,则返回 true。

_.isNull(null);
=> true
_.isNull(undefined);
=> false

回答by James Drinkard

This is what I use and it's the most concise. It covers: undefined, null, NaN, 0, "" (empty string), or false. We can therefore say that "object" is truthy.

这是我使用的,它是最简洁的。它涵盖:未定义、空值、NaN、0、""(空字符串)或 false。因此,我们可以说“对象”是真实的。

if(object){
    doSomething();
}

回答by Wex

Checking if foo === undefinedwill trigger the error foo is not defined. See variable === undefined vs. typeof variable === "undefined"

检查是否foo === undefined会触发错误foo is not defined。请参阅变量 === 未定义与 typeof 变量 === “未定义”

The existential operatorin CoffeeScript compiles to

CoffeeScript 中的存在运算符编译为

typeof face !== "undefined" && face !== null

Edit:

编辑:

Matt's commentis better if you only want to check for strings:

如果您只想检查字符串,Matt 的评论会更好:

typeof x === 'string' && x.length > 0

回答by jukempff

To check against nullAND undefinedAND "empty string" you can just write

要检查nullAND undefinedAND “空字符串”,您可以只写

if(!x) {
   // will be false for undefined, null and length=0
}

BUT you need to be sure that your variable is defined! Otherwise this will cause an error.

但是您需要确保您的变量已定义!否则这将导致错误。

If you are checking for a value in an object(e.g. the windowobject), you can always use that. E.g. for checking for localStoragesupport:

如果您正在检查object(例如window对象)中的值,您始终可以使用它。例如检查localStorage支持:

var supports = {
    localStorage: !!window.localStorage
}

回答by iJade

Try this

试试这个

 if (!x) {
  // is emtpy
}