Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between undefined and null in JavaScript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the difference between undefined and null in JavaScript". In daily operation, I believe that many people have doubts about the difference between undefined and null in JavaScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between undefined and null in JavaScript?" Next, please follow the editor to study!

1. Similarity

In JavaScript, assigning a variable to undefined or null, to be honest, makes little difference.

Var a = undefined;var a = null

In the above code, the a variable is assigned to undefined and null, which are almost equivalent.

Both undefined and null are automatically converted to false in the if statement, and the equality operator even directly reports that they are equal.

If (! undefined) console.log ('undefined is false'); / / undefined is falseif (! null) console.log (' null is false'); / / null is falseundefined = = null// true

The above code shows how similar the two behaviors are!

Since both undefined and null have the same meaning and usage, why set two such values at the same time? doesn't this increase the complexity of JavaScript for no reason and bother beginners? Dart, an alternative to the JavaScript language developed by Google, makes it clear that there is only null, not undefined!

II. Historical reasons

Recently, when I was reading my new book Speaking JavaScript, I came across the answer to this question!

It turns out that this has something to do with the history of JavaScript. When JavaScript was born in 1995, just like Java, only null was set as the value for "none".

According to the tradition of the C language, null is designed to automatically convert to 0.

Number (null) / / 05 + null// 5

But Brendan Eich, the designer of JavaScript, feels that this is not enough for two reasons.

First of all, null is treated as an object, as in Java. However, the data types of JavaScript are divided into two categories: primitive type (primitive) and composite type (complex), and Brendan Eich thinks that the value that represents "none" is not an object.

Second, the original version of JavaScript did not include an error handling mechanism, and when a data type mismatch occurred, it was often automatically converted or silently failed. Brendan Eich feels that if null automatically turns to 0, it is not easy to find errors.

So Brendan Eich designed another undefined.

III. Original design

The original version of JavaScript is distinguished as follows: null is an object that represents "none", 0 is undefined when converted to a numeric value, is a primitive value that represents "none", and NaN when converted to a numeric value.

Number (undefined) / / NaN5 + undefined// NaN

IV. Current usage

However, the above distinction quickly proved to be infeasible in practice. Currently, null and undefined are basically synonymous, with only minor differences.

Null means "no object", that is, there should be no value there. Typical usage is:

(1) as a parameter of a function, it indicates that the parameter of the function is not an object.

(2) as the end point of the object prototype chain.

Object.getPrototypeOf (Object.prototype) / / null

Undefined stands for "missing value", which means there should be a value here, but it hasn't been defined yet. Typical usage is:

(1) when a variable is declared, but it is not assigned, it is equal to undefined.

(2) when calling the function, the parameter that should be provided is not provided, which is equal to undefined.

(3) the object has no property assigned to it, and the value of this property is undefined.

(4) when the function does not return a value, it returns undefined by default.

Var I / / undefinedfunction f (x) {console.log (x)} f () / / undefinedvar o = new Object (); O.P / / undefinedvar x = f (); x / / undefined at this point, the study on "what's the difference between undefined and null in JavaScript" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report