In fact, I've listed all the "null" and "false" values in JavaScript, except that the Boolean value itself is true and false, "null values" for other data types are mainly undefined and defined. The types of these null values are:
The code is as follows |
Copy Code |
typeof (undefined) = ' undefined ' typeof (null) = = ' object ' typeof ("") = = ' String ' typeof (0) = = ' Number ' typeof (false) = = ' Boolean ' |
What these five values have in common is that the false branch is executed in the IF statement. In broad terms, of course, these values are either invalid values or null values on their corresponding data types. And these five values are done! operation, the result is all: true.
These values are also different, where undefined and null are special, although the type of NULL is object, but Null does not have any attributes of any object, which means that we are not able to perform null.tostring (), The default invocation of object instances such as Null.constructor. So in this sense, null and undefined have the greatest similarity. The result of the NULL = = undefined (true) is much more descriptive. But similarity is similar, there is a difference, that is, and the number of operations, the result of a + null: 10;10 + undefined result is: NaN.
In addition, "", 0, and false, although the if statement is represented as "false value", they are all meaningful data and are only used as "null value" or "False value" because: "". ToString (), (0). ToString () and false.tostring () are valid executable expressions.
In fact, these 5 values in the above mentioned differences, not too much to the process control caused by the problem, so what to distinguish them? Note that the difference between these values when converting to string is relatively large, and their conversion to string is:
code is as follows |
copy code |
String (undefined)-> "undefined" string (NULL)-> "null" string ("")-> "" string ( 0)-> "0" string (False)-> "false" |
So, after thinking carefully, write down this article, please heroes to shoot bricks.
Generally known:
Null = = undefined
But:
Null!== undefined
So what's the difference between the two?
Null
This is an object, but is empty. Because it is an object, typeof null returns ' object '.
Null is a JavaScript reserved keyword.
Null is automatically converted to 0 when it participates in numeric operations, so the following expression evaluates to the correct value:
Expression: 123 + null result value: 123
Expression: 123 * Null result value: 0
Undefined
Undefined is a special property of the Global Object (window) whose value is undefined. But typeof undefined returned to ' undefined '.
Although undefined has a special meaning, it is indeed a property and is a property of the Global Object (window). Take a look at the following code:
At present, null and undefined are basically synonymous, with only a few subtle differences. Null means that there is no object, that is, there should be no value in the place. Typical usage:
As a parameter to the function, which indicates that the parameter of the function is not an object.
As the endpoint of the object's prototype chain.
The code is as follows |
Copy Code |
Object.getprototypeof (Object.prototype)
|
Null
Undefined indicates a missing value, where there should be a value, but not yet defined. Typical usage:
When a variable is declared, it is equal to undefined when it is not assigned a value.
When calling a function, the argument that should be supplied is not supplied, which equals undefined.
Object has no assigned property, the value of this property is undefined.
Returns undefined when the function does not return a value.
code is as follows |
copy code |
var i; Console.log (i); Undefined function f (x) {console.log (x);} F ();//undefined var o = new Object (); O.P;//undefined function x = f (); x;//undefined | /tr>