In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about the use of null in javascript. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Null in javascript is a special value provided by JS to indicate missing objects; in javascript, you can use the strict equality operator to check for null values, such as "missingObject = null;".
This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer.
What is null in javascript?
Everything about null in JavaScript
There are two types of JavaScript: basic types (string, booleans number, symbol) and objects.
Objects are complex data structures, and the simplest objects in JS are ordinary objects: a set of keys and associated values:
Let myObject = {name: 'front-end intelligence'}
In some cases, however, objects cannot be created. In this case, JS provides a special value of null-which indicates that the object is missing.
Let myObject = null1. The concept of null
The value null means that the value of the object is not set. It is one of the basic types of JS and is considered to be falsy in Boolean operations.
For example, the function greetObject () creates an object, but can also return null if the object cannot be created:
Function greetObject (who) {if (! who) {return null;} return {message: `Hello, ${who}! `};} greetObject ('Eric'); / / = > {message:' Hello, riches'} greetObject (); / / = > null
However, when the function greetObject () is called without arguments, the function returns null. It is reasonable to return null because the who parameter has no value.
two。 How to check null
A good way to check for null values is to use the strict equality operator:
Const missingObject = null;const existingObject = {message: 'Hellobirds'}; missingObject = null; / / = > trueexistingObject = null; / / = > false
The result of missingObject = = null is true because the missingObject variable contains a null value.
If the variable contains a non-null value, such as an object, the expression existObject = = null evaluates to false.
2.1 null is an imaginary value
Null and false, 0,', undefined, NaN are all imaginary values. If an imaginary value is encountered in a conditional statement, JS forces the imaginary value to false.
Boolean (null); / / = > falseif (null) {console.log ('null is truthy')} else {console.log (' null is falsy')} 2.2 typeof null
What is the result of type null?
Typeof null; / / = > 'object'
Why 'object',typoef null is object' is an error in early JS implementations.
To detect null values using the typeof operator. As mentioned earlier, use the strict equal operator myVar = null.
If we want to use the typeof operator to check whether the variable is an object, we also need to exclude null values:
Function isObject (object) {return typeof object = = 'object' & & object! = = null;} isObject ({prop:' Value'}); / / = > trueisObject (15); / / = > falseisObject (null); / / = > false3. The trap of null
Null often occurs unexpectedly when we think that the variable is an object. Then, if you extract the attribute from null, JS throws an error.
So when you want to get properties from an object, you should be careful to judge that the object may be null, otherwise it may make an error.
[recommended study: "javascript basic course"]
4. Alternative methods of null
When an object cannot be constructed, our usual practice is to return null, but this has its drawbacks. When a null appears in the execution stack, it must just be checked.
Try to avoid returning null:
Returns the default object instead of null
Throw an error instead of returning null
5. Null vs undefined
Undefined is the value of an uninitialized variable or object property
The main difference between null and undefined is that null represents a lost object, while undefined represents an uninitialized state.
Tip: after the article is written, the directory can be generated automatically, how to generate the help documentation on the right.
Thank you for reading! This is the end of this article on "what is the use of null in javascript?". I hope the above content can be of some help to you, so that you can 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.