Speaking of null and undefined, I believe many people are both familiar and confused.
Null is a special value that is commonly used to describe "null", which can represent numbers, strings and objects as "no value", and null execution typeof will get "object".
Undefined is a value of the variable, indicating that the variable is not initialized, and undefined execution typeof will be "undefined".
Because both represent "empty values", the equality operator "= =" is judged to be equal, and the result is true, which requires "= = =" To distinguish between the two, which returns false at that time.
Null should be used in the following scenario:
1. Used to initialize a variable, this variable may be assigned to an object;
2. To compare with an already initialized variable, this variable can or may not be an object;
3. When the function parameter is expected to be an object, it is used as a parameter;
4. When the return value of a function is expected to be an object, it is used as the return value.
Null should not be used in the following scenario:
1. Do not use NULL to detect whether a parameter is passed in;
2. Do not use NULL to detect an uninitialized variable.
The best way to understand null is to treat it as a placeholder for the object.
Those variables that are not initialized have an initial value of "undefined", which means that the variable waits for the value to be assigned.
So it's good practice to assign an initial value to the variable so that you can effectively ensure that typeof returns "undefined" only in one case: when the variable is not declared.
null and undefined