简体   繁体   English

在 javascript 中使用 != null 和 != undefined 之间有什么功能区别吗?

[英]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 != undefinedx != null都只在xnullundefined时计算为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.不要混淆undefinednull因为它们不是一回事。

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 !但是,测试变量是否为nullundefined的更好方法是使用! negation as the value null or undefined will be resolved to true.否定,因为值nullundefined将被解析为 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.

相关问题 JavaScript中的null和undefined之间的区别? - difference between null and undefined in JavaScript? 使用上有什么区别吗!! vs 使用 null 检查 | 不明确的? 在 javascript/打字稿中 - Is there any difference in using !! vs using checks for null | undefined? in javascript/typescript null 和 JavaScript 中的 undefined 有什么区别? - What is the difference between null and undefined in JavaScript? JavaScript 检查 null 与 undefined 以及 == 和 === 之间的区别 - JavaScript checking for null vs. undefined and difference between == and === javascript中的({})和{}之间有什么区别吗? - Is there any difference between ({}) and {} in javascript? Javascript:DOM元素和对象之间的区别/“未定义不是函数” - Javascript: Difference Between DOM elements and Objects/“Undefined Is Not A Function” javascript函数中显式和隐式返回之间的任何区别? - Any difference between explicit and implicit return in javascript function? 跑步之间有什么区别 <script></script> 和function()JavaScript? - is any difference between running <script></script> and function() javascript? JavaScript 对象中的不同函数声明之间有什么区别(如果有)? - What is the difference (if any) between the different function declarations within JavaScript objects? 使用 Javascript 递归检查 object 中的任何 null/未定义键/值 - Recursively check for any null / undefined keys / values in an object using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM