当前位置:网站首页>The difference between null and undefined
The difference between null and undefined
2022-04-21 18:58:00 【As cool as mushrooms】
First look at javascript Seven types of
1.Undefined( Basic types )
2.Null( Basic types )
3.Boolean( Basic types )
4.String( Basic types )
5.Number( Basic types )
6.Symbol( Basic types )
7.Object( Reference type )
It can be seen that ,Undefined and Null yes Javascript Two special raw data types in (Primary Type), They all have one value , They correspond to each other undefined and null , These two different types of values , It has different semantics and scenes , And show more similar behavior :
undefined
undefined Literally : Undefined value . The meaning of this value is , hope Represents the original state of a variable , Not the result of human operation . This primitive state will be in the following 4 There are kinds of scenes :
【1】 Declared a variable , But there is no assignment
| 1 2 |
|
visit foo, Back to undefined, Indicates that this variable has been declared since , I've never used , No valid values have been defined , That is, in a primitive and unavailable state .
【2】 Access to properties that do not exist on the object
| 1 2 3 |
|
visit Object On the object foo attribute , return undefined , Express Object Does not exist on or does not define the name foo Properties of . The elements in the array also belong to the object properties inside , To access the subscript is to access this property , return undefined , It means that this element does not exist in the array .
【3】 Function defines parameter , But no arguments were passed
| 1 2 3 4 5 |
|
function fn The formal parameter is defined a, but fn No parameters passed when called , therefore ,fn Runtime parameters a It's a primitive 、 Unassigned variables .
【4】 Use void Evaluate expressions
| 1 2 3 4 5 |
|
ECMAScript Make it clear void The operator Evaluate any expression to return undefined , This is the same as a function that does not return a value after it performs an operation ,JavaScript All functions in have return values , When there is no return In operation , By default, it returns an original state value , This value right here is undefined, Indicates that the return value of the function is undefined .
therefore ,undefined It usually comes from the original state value of an expression , It's not the result of human operation . Of course , You can also assign a value to a variable manually undefined, But it doesn't make sense , Because a variable that is not assigned is undefined .
null( It is also the end of the object prototype chain )
null Literally, it means : Null value . The meaning of this value is , hope Express An object is artificially reset to an empty object , Not the most primitive state of a variable . The expression in memory is , Variables in the stack don't point to memory objects in the heap , namely :
Drawing :

When an object is assigned null in the future , The original object is in a free state in memory ,GC It will recycle the object and free the memory . therefore , If you need to release an object , Just set the variable to null, This means that the object has been cleared , Currently invalid . Just imagine , If you put null Switch to undefined Will you feel uncomfortable ? Obviously, the semantics are not clear , Its operation cannot correctly express its desired behavior .
And null Another related question needs to be explained :
| 1 |
|
null Have their own type Null, It doesn't belong to Object type ,typeof The reason for this is that Object type , Because JavaScript Data types are represented in binary form at the bottom , The first three bits of binary are 0 Will be typeof Judge as object type , and null The binary bits of are exactly 0 , therefore ,null Be misjudged as Object type .
000 - object , Data is the application of objects
1 - integer , The data is 31 Bit signed integer
010 - Double precision type , The data is double precision
100 - character string , Data is a string
110 - Boolean type , Data is Boolean
Actually , We can get... In another way null The real type of :
| 1 |
|
adopt Object Archetype toString() Method can get JavaScript The real data type of the object in , Of course undefined Types can also be obtained in this way :
| 1 |
|
similarity
although undefined and null Different semantics and scenarios , But all in all , They all represent a Invalid value . therefore , stay JS When accessing properties for such values in , All of them will get abnormal results :
| 1 2 |
|
ECMAScript Normative view , since null and undefined The behavior is very similar , And they all mean An invalid value , So what they represent is similar , That is to say
| 1 |
|
Don't try to explain this conclusion by converting data types , because :
| 1 2 3 4 5 |
|
but === Returns the false , Because full operation === When comparing equality , The data type of the item will not be actively converted , And they don't belong to the same type :
| 1 2 |
|
summary
In a word, the difference between them is :undefined It means that a variable is natural 、 The most primitive state value , and null It means that a variable is artificially set as an empty object , Not the original state . therefore , In actual use , To ensure the semantics represented by variables , Don't assign an explicit value to a variable undefined, When you need to release an object , The direct assignment is null that will do .
Detailed explanation undefined And null The difference between - A pixel - Blog Garden
版权声明
本文为[As cool as mushrooms]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211852430697.html
边栏推荐
猜你喜欢

看机器人教育二十一世纪之变

Read the meaning of seek tiger's launch of Genesis node

Overview of u-net network deformation

Abbexa 驴抗山羊 IgG (H&L) 抗体

Chinese NER Using Lattice LSTM 论文解读

SPFA determines whether there is a negative ring

CheKine 系列高密度脂蛋白(HDL-C)含量检测方案

108. 将有序数组转换为二叉搜索树(图文并解)

el-tabs中套用el-tooltip(气泡文字提示框)

真无线耳机什么牌子好?高颜值旗舰蓝牙耳机
随机推荐
Chinese NER Using Lattice LSTM 论文解读
EasyGBS关闭了录像计划,为何还有录像文件生成?
[04] [02] [02] SPI mechanism
Typescript quick start, class, public, private, extensions
ssh-keygen 设置免密登录
86 R k-means,层次,EM聚类介绍
Which brand of wireless Bluetooth headset is good? Wireless Bluetooth headset recommendation
Abbexa 山羊 F(ab‘)2 IgG 同种型对照
干货 | 面试过了,如何谈薪资?拿高薪Offer有哪些技巧?
无线蓝牙耳机哪个品牌好?无线蓝牙耳机推荐
Ubuntu install go
Database advanced learning: introduction to index and BTREE, B + tree, hash
SPFA determines whether there is a negative ring
How does PR open MKV files? How can MKV files be converted to MP4 and how can MP4 be consumed by ImageJ?
Svg series - 1, entry level operation
【无标题】
解读自主形机器人的责任感
体育场馆系统可以连接其他智能设备吗
Elementary mathematical modeling problem
牛客 - 另类加法