In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what is the difference between null and undefined in JavaScript. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
There is a lot of discussion about the difference between null and undefined on the Internet, which seems to say a lot of knowledge, but it seems to be useless and unable to guide the actual development. Most of the conclusions are that null is basically the same as undefined, with only slight differences, is the product of historical design errors, and even one of them should be removed from the language.
In fact, there is a big difference between the two values, and there is a meaning for the existence of the two values.
Undefined
Undefined stands for undefined, which means it doesn't exist. You can also change the angle, that is, there should be a value, but it hasn't been assigned yet, and it hasn't even been assigned a null value.
Var foo;console.log (foo); / / undefinedfoo = null;console.log (foo); / / nullconsole.log (bar); / / Uncaught ReferenceError: bar is not definednull
Null means there is a value, it exists, but it is null. A null value does not exist, but there is a value called a null value. At the language level, null is an object, a special empty object. If you assign a variable to null, the object that the variable originally points to can be reclaimed by GC, freeing up memory space. Of course, you can also think of null as a null type, which does not affect the actual meaning.
Console.log (typeof (null)); / / objectconsole.log (typeof (undefined)); / / undefinedundefined and null types
In TypeScript, there are undefined and null types, both of which have only one value, which is themselves. And is a subtype of all other types and can be assigned to any type.
Let u: undefined = undefined;let n: null = null;let num: number = u; practical application
With all that said above, it seems to be of no practical use except to add complexity.
The following code demonstrates the actual use of these two types, and you can see the difference and usefulness between non-existence and null values:
Var foo = {avell 1, foo 2}; console.log (JSON.stringify (foo)); / / {"a": 1, "b": 2} foo = {... foo, a:null}; console.log (JSON.stringify (foo)); / / {"a": null, "b": 2} foo = {... foo, a:undefined}; console.log (JSON.stringify (foo)); / / {"b": 2}
As you can see from the above code, when an object's property is set to undefined, this property disappears and does not exist. When set to null, this property still exists, but it is a null value.
If you want a property in the object to disappear, set it to undefined. This is perhaps the greatest significance of the co-existence of undefined and null.
What is JavaScript? JavaScript is a literal scripting language whose interpreter is called JavaScript engine and is a part of the browser. JavaScript is a scripting language widely used on the client side. It was first used on HTML pages to add dynamic functions to HTML pages.
So much for sharing what is the difference between null and undefined in JavaScript. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.