首页 > 编程语言 > 详细

JavaScript Null 和 Undefined ?

时间:2020-06-15 16:37:24      阅读:43      评论:0      收藏:0      [点我收藏+]

Null和Undefined是JavaScript里面的两种数据类型。

Undefined is a variable that has been declared but not assigned a value.

Null as an assignment value. So you can assign the value null to any variable which basically means it’s blank.

So by not declaring a value to a variable, JavaScript automatically assigns the value to undefined. However, when you assign null to a variable, you are declaring that this value is explicitly empty.

技术分享图片
技术分享图片

Null and Undefined

JavaScript will never automatically assign the value to null. That must be done by you in your code.

Let’s get some more info on these.

Typeof Null and Undefined

技术分享图片
技术分享图片

Typeof null and undefined

We see here that the type of null is an object but the type of undefined is undefined.

Comparison Using Abstract and Strict Equality

Since these are different data types, if we compare them with strict equality ===, we get false.

技术分享图片
技术分享图片

Comparison between null and undefined

But if we compare them with abstract equality ==, we get true.

So JavaScript does consider these to be relatively equal since they both represent an empty value.

技术分享图片
技术分享图片

So if you need to check if a value is either null or undefined, you can check for abstract equality and compare it to either null or undefined. Both will return the same result.

 

 

转载自:https://levelup.gitconnected.com/javascript-null-vs-undefined-2acda986f79f

 

JavaScript Null 和 Undefined ?

原文:https://www.cnblogs.com/spring87/p/13131507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!