This is the first day of my participation in the August More text Challenge. For details, see: August More Text Challenge

preface

This series will be divided into several articles on js related “unpopular knowledge”, here “unpopular knowledge” is the knowledge that most developers do not pay attention to, or is unexpected knowledge. These knowledge points may not be very helpful in the real interview, development, or even any use at all, but I hope to make everyone happy, and to expand the scope of knowledge.

Most of the knowledge in this series is based on the wangdoc.com documentation written by Yifeng Ruan. I strongly recommend that web front-end developers read this documentation, which is both a good helper for getting started and a useful tool for expanding their knowledge.

Any references to this series will be indicated at the end of the article.

The sample articles are all verified by me. If there are any mistakes, please correct them in the comments section.

Why null and undefined

As anyone who has done development knows, both dynamically typed and statically typed languages require something that means “nothing”, such as Null in Java, None in Python, and so on.

In JS, there are also “nothing” things, and there are two of them, null and undefined. Why are there two data types that represent “none”?

It’s about history.

In the first version of JS, THE js designer Brendan Eich designed null, but later he decided that null was not enough to mean “nothing” for two reasons:

  1. Brendan Eich thinks that JS is divided into primitive type and compound type (object, array, function is collectively called compound type (complex)). It is better that the value representing “nothing” is not an object, but it is originally designednullWill be treated as an object by JS.
typeof null // "object"
Copy the code
  1. nullCan beNumber()Function is converted to 0, and js’s “automatic conversion type” is usedNumber()Function, Brendan Eich thinksnullWill be automatically converted to 0, because the original JS did not have error handling mechanism, it is not easy to find the error.
var obj = null
1 + obj // Result = 1, not 1[object object]
Copy the code

So, Brendan Eich decided to add a new data type, undefined, that represents “nothing.”

Undefined, as we all know, means “undefined” or “undeclared.” If a variable is not declared, its value is undefined, not null.

  1. undefinedWill betypeofJudgment for"undefined"
typeof undefined // "undefined"
Copy the code
  1. Number()Function willundefinedtoNaN.
Number(undefined) // NaN
Copy the code

Let’s look directly at how people interpret null and undefined.

Foreign bull Axel Rauschmayer puts it this way:

Null means “no object.” It is used as a nonvalue whenever an object is expected (parameters, last in a chain of objects, etc.).

Undefined means “no value.” Uninitialized variables are undefined

Ruan yifeng interprets it like this:

Null means “no object”, meaning there should be no value there.

Undefined means “missing value,” meaning there should be a value here, but it’s not defined yet.

Here, Axel Rauschmayer and Ruan yifeng read it very well, I don’t need to read it any more.

Null and undefined

Null == undefined true?

Yes, if you compare null and undefined with the non-strict equality operator, the program thinks the two are equal. But if you use the strict equality operator, the program says they’re not equivalent.

null= =undefined // true
null= = =undefined // false
Copy the code

I also dug up some answers to null and undefined from JS designer Brendan Eich on Twitter. My English is not good, my opinion is for reference only (the following personal opinion is also for reference only, please refer to Brendan Eich’s original answer more).

According to Brendan Eich, something like this. Null and undefined in js == are considered to be from the equivalent type.

Why is null so difficult to use in JS

I don’t understand the relevance of what the author is saying to the question. Something is not right……

Brendan Eich doesn’t like the null and undefined design

In fact, Brendan Eich himself didn’t like the null and undefined design at all, and it was only to cope with the Netscape hierarchy that JS became more like Java (Brendan Eich didn’t like Java). Brendan Eich specifically noted on Twitter that he prefers an “everything is an object” design, so there are no Boolean, number, string wrappers, undefined, and null.

Supplement: The irrationality of JS is basically caused by historical reasons

As for the history of JS, I think everyone in the front end should understand why JS seems so strange in some places compared to other languages. In fact, it is all related to history.

Check out ruan Yifeng’s blog (ruanyifeng.com) for this article.

conclusion

null

  1. nullIs a special data type, but it will betypeofTo judge as objects, butnullThere are no object properties or methods (such as toString).
typeof null // "object"
null.toString() // Uncaught TypeError: Cannot read property 'toString' of null
// However, instanceof does not consider null to be an Object constructor instance
null instanceof Object // false
Copy the code
  1. nullIn an automatic data type conversion scenario, it is automatically converted to 0.
1 + null // = 1, not 1[object object]
Copy the code
  1. nullMeans “no object”, meaning there should be no value there. (Summary by Ruan Yifeng)

undefined

  1. undefinedIs a special data type that will betypeofJudgment forundefined.
typeof undefined // "undefined"
Copy the code
  1. undefinedWill beNumber()Function toNaN.
Number(undefined) // NaN
/ / so
1 + undefined // NaN
Copy the code
  1. undefined“Missing value” means there should be a value here, but it’s not defined yet. (Summary by Ruan Yifeng)

reference

  1. Null, undefined, and Boolean values
  2. The difference between undefined and null – ruanyifeng.com
  3. The Birth of Javascript – Ruan Yifeng’s blog (ruanyifeng.com)
  4. Speaking JavaScript by Dr. Axel Rauschmayer – Chapter 1. Basic JavaScript (SpeakingJs.com)
  5. Brendan Eich’s Twitter: Whynull == undefinedIs equal to the true
  6. Brendan Eich’s Twitter: Don’t like js data type design

Just for fun, Ruan yifeng really likes to read books by Dr. Axel Rauschmayer, and a lot of the content of the website is based on Axel Rauschmayer.

Only those who stand on the shoulders of giants are likely to see the farther view.

“Js unpopular knowledge” series of articles recommended

  1. Js Minor knowledge – tags

Pay attention to column, know more JS unpopular knowledge.