[英]Is there any function difference between using != null and != undefined in javascript?
In javascript, is there any functional difference between using != null
and != undefined
?在javascript中,使用!= null
和!= undefined
之间有什么功能区别吗?
Is there a value you can assign to myVar
which will cause these two lines of code to evaluate to different results?是否有可以分配给myVar
的值,它会导致这两行代码计算出不同的结果?
console.log(myVar != undefined)
console.log(myVar != null)
And if you know anything about the performance of these two operations, I'd love to know about that too.如果您对这两个操作的性能有所了解,我也很想知道。
There is no functional difference.没有功能差异。 x != undefined
and x != null
both only evaluate to false
when x
is null
or undefined
. x != undefined
和x != null
都只在x
为null
或undefined
时计算为false
。 They both evaluate to true for all other values of x
.对于x
所有其他值,它们都评估为真。
There is also no performance difference.也没有性能差异。
There is no difference as you can see in below table for JS ==
testing (focus on null/undefined row/column) (src: here ).正如您在下表中看到的 JS ==
测试(关注空/未定义的行/列)(src:此处)没有区别。 So myVar!=null
is true only if myVar
value is not null
and not undefined
(same with myVar != undefined
)因此myVar!=null
仅当myVar
值不为null
且未undefined
时才为真(与myVar != undefined
相同)
It looks like both has similar performance (I made test on Mac OS X 10.13.4 HighSierra: Chrome 71.0.3578, Firefox 65.0.0 and Safari 11.1.0 - you can run test in your browser here )看起来两者都有相似的性能(我在 Mac OS X 10.13.4 HighSierra 上进行了测试:Chrome 71.0.3578、Firefox 65.0.0 和 Safari 11.1.0 - 您可以在此处在浏览器中运行测试)
let myVar1=null;
let myVar2=undefined;
The ==
and !=
operators make "type conversion" to compare just the value itself. ==
和!=
运算符进行“类型转换”以仅比较值本身。 Then no, there's no difference using "undefinied" or "null" is this case, both represents "empty".那么不,在这种情况下使用“未定义”或“空”没有区别,两者都代表“空”。
But if you use ===
and !==
instead, it checks the type and the value, without any type casting.但是如果你使用===
和!==
,它会检查类型和值,而不进行任何类型转换。 The results for the two lines would be different.两条线的结果将不同。
myVar = null;
console.log(myVar !== undefined) //true
console.log(myVar !== null) //false
Don't confuse undefined and null as they are not the same thing.不要混淆undefined和null因为它们不是一回事。
null:空值:
The value null represents the intentional absence of any object value.值 null 表示有意缺少任何对象值。 It is one of JavaScript's primitive values.它是 JavaScript 的原始值之一。
undefined:不明确的:
A variable that has not been assigned a value is of type undefined.尚未赋值的变量的类型为 undefined。 A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value.如果正在评估的变量没有赋值,方法或语句也会返回 undefined。 A function returns undefined if a value was not returned.如果未返回值,则函数返回 undefined。
If the variable consists of a value that is neither null
nor undefined
, then no there is no difference in your condition.如果变量包含的值既不是null
也不是undefined
,那么您的条件没有区别。
const value = 3; console.log(value !== undefined) //true console.log(value !== null) //true
However, a better way of testing if the variable is null
or undefined
is to use !
但是,测试变量是否为null
或undefined
的更好方法是使用!
negation as the value null
or undefined
will be resolved to true.否定,因为值null
或undefined
将被解析为 true。
const undefinedValue = undefined; const nullValue = null; console.log(!undefinedValue); console.log(!nullValue);
Here are some examples of it.这里有一些例子。
var someVariable = undefined; console.log(someVariable !== undefined, "; undefined !== undefined"); console.log(someVariable !== null, "; undefined !== null"); var someVariable = null; console.log(someVariable !== undefined, "; null !== undefined"); console.log(someVariable !== null, "; null !== null"); var someVariable = undefined; console.log(!someVariable); var someVariable = null; console.log(!someVariable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.