Null and undefined are the most common null problems in JavaScript.

The definition of null and undefined

The original version of JavaScript was distinguished as follows:

  • Null is an object representing “none” and is zero when converted to a value;
  • Undefined is a primitive value for “none”, which is NaN when converted to a value.

Below is a comparison of the two concepts above:

Why is this a problem

This has to do with the language nature of JavaScript.

In the beginning, JavaScript is often defined as an interpreted language. The feature of this language is that it does not need to be compiled, but interpreted directly.

In order to enhance the flexibility of the language, JavaScript is also designed as a weakly typed language.

In other words, when null is defined above, it can directly correspond to the integer type, which causes a lot of confusion when executing the code.

This can be a headache for compiled types and strongly typed languages.

For example, in Java, if you want to use a string, you have to define the string, you can null the string, or initialize some characters, but you have to define.

If you don’t define a variable at all and use it directly, you’ll get undefined.

In strongly typed languages, if you define a variable as a string, then you can only store strings in that variable. If you store other types, the compiler will report an error. JavaScript will try to automatically cast you, which can cause a lot of confusion, such as storing a 1 in a variable, which can be an integer 1, or it can be a Boolean.

How to determine

You can use the _.isnil () function to determine this.

Check whether value is null or undefined.

Note that there is no way to tell if your input value is “.

www.ossez.com/t/javascrip…