Null is a special value, but we often misunderstand it and confuse it with undefined.

Null should be used in the following scenarios.

1. Used to initialize a variable that may be assigned to an object. 2. To compare with an initialized variable, which may or may not be an object. 3. Used as an argument pass when the function’s argument is expected to be an object. 4. When the return value of a function is expected to be an object, it is used as the return value. There are also some scenarios where NULL should not be used. 1. Do not use NULL to detect whether a parameter has been passed in. 2. Do not use NULL to detect an uninitialized variable.

Here’s some sample code:

Undefined is also a special value, which is often confused with null.

One of the confusing things is that null==undefined results in true. However, the two values serve different purposes. Variables that are not initialized have an initial value, undefined, indicating that the variable is waiting to be assigned.

Three, notes

1. Undefined and undeclared variables, typeof operation results are “undefined”. The typeof operator returns “object” when it operates on a null type. 2. By disallowing the use of the special value undefined, you effectively ensure that typeof returns undefined only in one case: when the variable is undeclared. This can distinguish null from undefined.