False, 0, null, undefined, and empty string objects in Javascript

Source: Internet
Author: User
Tags throw exception

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?
    1. <script Type= "Text/javascript" > 
    2.     alert (typeof (false) = = = ' Boolean ');  
    3.      Alert (typeof (0) = = = ' number '),  
    4.     alert (typeof ("") = = = ' string ');  
    5.     alert (typeof (null) = = = ' object ')  
    6.     alert (typeof span class= "attribute" >undefined = = = ' undefined ');     
    7. </script> 
<script type="text/javascript">
    alert(typeof(false) === ‘boolean‘);
    alert(typeof(0) === ‘number‘);
    alert(typeof("") === ‘string‘);
    alert(typeof(null) === ‘object‘);
    alert(typeof undefined === ‘undefined‘);    

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?
    1. <script Type= "Text/javascript" > 
    2.     alert (false = = undefined); 
    3.     alert (false = = null); 
    4.     Alert (false = = 0); 
    5.     alert (false = =" "); 
    6.     alert (null = = undefined); nbsp
    7. </script> 
<script type="text/javascript">
    alert(false == undefined);
    alert(false == null);
    alert(false == 0);
    alert(false == "");
    alert(null == undefined);

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?
    1. <script Type= "Text/javascript" > 
    2.     alert (false.tostring ());   //"false" &NBSP;
    3.     alert ("". CharAt (0));       //""  
    4.     Alert ((0) toexponential); //0.0000000e+0 
    5.  & nbsp;  alert (undefined.tostring ());   //Throw exception "Undefined has no properties" &NBSP;
    6.     alert (null.tostring ());        //"NULL has no Properties "&NBSP;
    7. </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"
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?
  1. <script type="Text/javascript">
  2. Alert (String (false)); "False"
  3. Alert (String ("")); // ""
  4. Alert (String (0)); 0.0000000e+0
  5. Alert (String (undefined)); "Undefined"
  6. Alert (String (null)); "NULL"
  7. Alert (decodeURI (undefined));//"Undefined"
  8. Alert (decodeURI (null));//"NULL"
  9. </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"
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?
  1. <script type="Text/javascript">
  2. var ar = [undefined,false,0, "", NULL];
  3. for (var i = 0,len = ar.length; I < len; i++) {
  4. if (Ar[i]) {
  5. Alert ("You should not see this dialog box!");
  6. }
  7. }
  8. </Script>
<script type="text/javascript">
     Var ar = [undefined,false,0,"",null];
     For(var i = 0,len = ar.length; i < len; i++){
             Alert ("You should not see this dialog!");

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?
  1. <script type="Text/javascript">
  2. var undefinedvariable,nullvariable = null;
  3. alert (undefinedvariable); "Undefined"
  4. alert (window.undefinedvariable); "Undefined"
  5. alert (WINDOW.ABCD); "Undefined"
  6. alert (nullvariable); "NULL"
  7. alert (ABCD); Throw exception "ABCD is not defined"
  8. </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"

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?
    1. <script type="Text/javascript">
    2. alert (1+undefined); "NaN"
    3. alert (1+null); "1"
    4. </Script>
<script type="text/javascript">
    alert(1+undefined);    // "NaN"
    alert(1+null);             // "1"

This is well understood in the sense of null and undefined.

False, 0, null, undefined, and empty string objects in Javascript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.