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:
[HTML]View Plaincopyprint?
- <script Type= "Text/javascript" >
- alert (typeof (false) = = = ' Boolean ');
- Alert (typeof (0) = = = ' number '),
- alert (typeof ("") = = = ' string ');
- alert (typeof (null) = = = ' object ')
- alert (typeof span class= "attribute" >undefined = = = ' undefined ');
- </script>
<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 equal to false;undefined and null objects are not equal to false objects, and null and undefined are equal
[HTML]View Plaincopyprint?
- <script Type= "Text/javascript" >
- alert (false = = undefined);
- alert (false = = null);
- Alert (false = = 0);
- alert (false = =" ");
- alert (null = = undefined); nbsp
- </script>
<script type="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:
[HTML]View Plaincopyprint?
- <script Type= "Text/javascript" >
- alert (false.tostring ()); //"false" &NBSP;
- alert ("". CharAt (0)); //""
- Alert ((0) toexponential); //0.0000000e+0
- & nbsp; alert (undefined.tostring ()); //Throw exception "Undefined has no properties" &NBSP;
- alert (null.tostring ()); //"NULL has no Properties "&NBSP;
- </script >
<script type="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.
[HTML]View Plaincopyprint?
-
- <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>
<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:
[HTML]View Plaincopyprint?
-
- <script type="Text/javascript">
-
- var ar = [undefined,false,0, "", NULL];
-
- for (var i = 0,len = ar.length; I < len; i++) {
-
- if (Ar[i]) {
-
- Alert ("You should not see this dialog box!");
-
- }
-
- }
-
- </Script>
<script type="text/javascript">
Var ar = [undefined,false,0,"",null];
For(var i = 0,len = ar.length; i < len; i++){
If(ar[i]){
Alert ("You should not see this dialog!");
}
}
</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, and null 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.
[HTML]View Plaincopyprint?
-
- <script type="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>
<script type="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.
[HTML]View Plaincopyprint?
- <script type="Text/javascript">
- alert (1+undefined); "NaN"
- alert (1+null); "1"
- </Script>
<script type="text/javascript">
alert(1+undefined); // "NaN"
alert(1+null); // "1"
</script>
This is well understood in the sense of null and undefined.
False, 0, null, undefined, and empty string objects in Javascript