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:

<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

<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:

<script type= "Text/javascript" >    alert (false.tostring ());    "False"    alert ("". CharAt (0));        ""    Alert ((0). Toexponential (Ten));  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:

<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>

  

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.

<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.

<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

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.