In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares to you is about the introduction and usage of null in JavaScript. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Without saying much, let's take a look at it with the editor.
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 = null
Here we'll learn everything about null in JavaScript: what it means, how to detect it, the difference between null and undefined, and why using null makes code maintenance difficult.
1. The concept of null
The JS specification describes information about 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; / / = > true existingObject = 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); / / = > false if (null) {console.log ('null is truthy')} else {console.log (' null is falsy')}
2.2 typeof null
The type of value determined by the typeof value operator. For example, typeof 15 is' number',typeof {prop:'Value'}, which evaluates to 'object'.
Interestingly, 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'}); / / = > true isObject (15); / / = > false isObject (null); / / = > false
3. 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.
Use the greetObject () function again and try to access the message property from the returned object:
Let who =''; greetObject (who) .message; / / throws "TypeError: greetObject () is null"
Because the who variable is an empty string, this function returns null. A TypeError error is thrown when accessing the message property from null.
You can handle null by using an optional link with a null value merge:
Let who =''greetObject (who)? .message? 'Hello, Strangerbread' / / = > 'Hello, Strangerbread'
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
Go back to the greetObject () function that starts to return the greeting object. When a parameter is missing, you can return a default object instead of null:
Function greetObject (who) {if (! who) {who = 'Stranger';} return {message: `Hello, ${who}! `};} greetObject (' Eric'); / / = > {message: 'Hello, Ericsson'} greetObject (); / / > {message: 'Hello, Strangerbread'}
Or throw an error:
Function greetObject (who) {if (! who) {throw new Error ('"who" argument is missing');} return {message: `Hello, ${who}! `};} greetObject ('Eric'); / / = > {message:' Hello, riches'} greetObject (); / / = > throws an error
These two approaches can avoid the use of null.
5. Null vs undefined
Undefined is the value of an uninitialized variable or object property, and undefined is the value of an uninitialized variable or object property.
Let myVariable; myVariable; / / = > undefined
The main difference between null and undefined is that null represents a lost object, while undefined represents an uninitialized state.
The strict equality operator = = distinguishes null from undefined:
Null = undefined / / > false
The double equal operator = = considers null and undefined to be equal.
Null = = undefined / / = > true
I use the double equality operator to check whether the variable is null or undefined:
Function isEmpty (value) {return value = = null;} isEmpty (42); / / = > false isEmpty ({prop: 'Value'}); / / = > false isEmpty (null); / / = > true isEmpty (undefined); / / = > true
6. Summary
Null is a special value in JavaScript that represents the missing object, and the strict equality operator determines whether the variable is empty: variable = = null.
The typoef operator is useful for determining the type of variable (number, string, boolean). However, if null, typeof is misleading: the value of typeof null is' object'.
Null and undefined are equivalent to some extent, but null indicates that the object is missing and undefined has no initialized state.
The above is the introduction and usage of null in JavaScript. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.