In JavaScript, we are often exposed to the 5 more special objects mentioned in the topic-false,0, empty strings ,null , and undefined . These objects are easy to use, so you have to be careful when you use them.
Type detection
Let's come down and see what their type is:
<script type=
"text/javascript"
>
alert(
typeof
(
false
) ===
‘boolean‘
);
alert(
typeof
(0) ===
‘number‘
);
alert(
typeof
(
""
) ===
‘string‘
);
alert(
typeof
(
null
) ===
‘object‘
);
alert(
typeof undefined ===
‘undefined‘
);
</script>
|
Running the above code, the dialog box that pops up should display true. That is,false is a Boolean type object, 0 is a numeric type object, an empty string is a string type object, and Null is an object, undefined type, or undefined.
Mutual compatibility
When you compare false objects to other objects with the = = operator, you will find that only 0 and empty strings are equal to false;undefined and null objects are not equal to false objects , and null and undefined are equal
<scripttype=
"text/javascript"
>
alert(
false == undefined);
alert(
false ==
null
);
alert(
false == 0);
alert(
false ==
""
);
alert(
null == undefined);
</script>
|
We can classify 0, empty string and false as a class, called " false Value "; the null and undefined are classified as a class called " null value ". A false value is also a valid object, so you can use a type-related method such as ToString, and a null value does not. The following code throws an exception:
<scripttype=
"text/javascript"
>
alert(
false
.toString());
// "false"
alert(
""
.charAt(0));
// ""
alert((0).toExponential(10));
// 0.0000000e+0
alert(undefined.toString());
// throw exception "undefined has no properties"
alert(
null
.toString());
// "null has no properties"
</script>
|
String representation
Although a null value cannot call the ToString method, it can be constructed using the string constructor . functions such as decodeURI, if passed in undefined or null, return "undefined" and "null" strings . This is easy to use wrong.
<script type=
"text/javascript"
>
alert(String(
false
));
// "false"
alert(String(
""
));
// ""
alert(String(0));
// 0.0000000e+0
alert(String(undefined));
// "undefined"
alert(String(
null
));
// "null"
alert(decodeURI(undefined));
// "undefined"
alert(decodeURI(
null
));
// "null"
</script>
|
False values and Null values as if conditions branch
False values and Null values have a common denominator , that is, when the conditional branch as if, are treated as false ; Apply "!" Is true after the Operation . The following example code:
<scripttype=
"text/javascript"
>
var ar = [undefined,
false
,0,
""
,
null
];
for
(
var i = 0,len = ar.length; i < len; i++){
if
(ar[i]){
alert(
"你不应该看到此对话框!"
);
}
}
</script>
|
This is because these objects are considered to be invalid values or null values in their respective types. So those objects in the If branch are treated as false.
The difference between null and undefined
The difference between these two null values is also confusing.
Undefined and null objects are nothing more than two special objects,undefined represents an invalid object , andnull represents an empty object . If the variable is explicitly or implicitly (assigned by the JavaScript engine) to undefined, then the variable is not defined, and if it is given a null value, it is initialized to a null value.
In JavaScript, a variable is defined by a var declaration, = an assignment (the object to which the variable is initialized). Of course, if you declare a global variable (as the Window property), you can not use the var keyword. Variables can be defined at the same time as the declaration.
<scripttype=
"text/javascript"
>
var undefinedVariable,nullVariable =
null
;
alert(undefinedVariable);
// "undefined"
alert(window.undefinedVariable);
// "undefined"
alert(window.abcd);
// "undefined"
alert(nullVariable);
// "null"
alert(abcd);
// throw exception "abcd is not defined"
</script>
|
In fact, if a variable is declared but not initialized, the JavaScript engine automatically points this variable to the undefined object.
It is important to note that when we refer to WINDOW.ABCD above, we pop the undefined, and when we refer to the ABCD variable directly, we throw an exception. This is because the JavaScript engine tries to find the variable from the nearest scope, and fails to find the variable that does not explicitly specify the chain of the object, and then fall back to the parent chain of action. If all lookups fail, an exception with "variable undefined" is thrown.
These two values are also different when doing a numeric operation :
<scripttype= "text/javascript" > alert(1+undefined); // "NaN" alert(1+ null ); // "1" </script> |
This is well understood in the sense of null and undefined.
"Reprint" http://www.imkevinyang.com/2009/07/javascript-%E4%B8%AD%E7%9A%84false%E9%9B%B6%E5%80%BCnullundefined%E5%92 %8c%e7%a9%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%af%b9%e8%b1%a1.html
False in Javascript, "0 value", "null", "Undefined", and "empty string"