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

Source: Internet
Author: User

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

In Javascript
, We often come into contact with the five special objects mentioned in the question -- false
, 0, empty string
, Null
And undefined
. These objects are easy to use incorrectly, so you must be careful when using them.

Type Detection

Let's see what their types are:

<Script Type= "Text/JavaScript">
Alert (Typeof(False) ='Boolean');
Alert (Typeof(0) ='Number');
Alert (Typeof("") ='String');
Alert (Typeof(Null) ='Object');
Alert (TypeofUndefined ='Undefined');
</Script>

Run the precedingCode, The pop-up dialog box should show true. That is, false is a Boolean object, 0 is a numeric object, null is a string object, null is an object, undefined or undefined.

Equality

When you use the = Operator to compare the false object with other objects, you will find that,Only 0 and null strings are equal to false (0 is the same as a null string.
); Undefined and null objects are not equal to false objects, while 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, null string, and false as a type, called "false value
"; Classifies null and undefined as one type, which is called" null value
". The dummy value is also a valid object, so you can use tostring and other type-related methods for it, while the Null Value
No. 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 the tostring method cannot be called for null values, the string constructor can be used for construction. for a function like decodeuri, if undefined or null is input, the returned values are "undefined" and "null" strings

. This is easy to use.

<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>
Leave value and null value as if condition Branch

There is a commonality between a false value and a null value, that is, inWhen it is used as the condition branch of if, it is regarded as false

; Application "! "True is obtained after the operation.

. Sample Code:

 
<ScriptType= "Text/JavaScript">
VaRAR = [undefined,False, 0,"",Null];
For(VaRI = 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 regarded as invalid values or null values in their respective types. Therefore, these objects in the IF branch are treated as false.

Differences between null and undefined

The differences between these two null values are also confusing.

The undefined and null objects are nothing more than two special
For example, undefined indicates an invalid object, and null indicates an empty object. If the variable is explicitly or implicitly assigned (assigned by the JavaScript engine) to undefined,
This variable is not defined. If null is assigned, it indicates that the variable is initialized as a null value.

In JavaScript, variables are defined using the VaR declaration, = value assignment (initialize the object pointed to by the variable ). Of course, if you declare a global variable (as the window attribute), you do not need to use the VaR keyword. Variables can be defined at the same time of declaration.

<ScriptType= "Text/JavaScript">
VaRUndefinedvariable, 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 the variable is declared but not initialized, the JavaScript engine automatically points to the undefined object.

Note that when we reference window. ABCD above, undefined is displayed. When we directly reference the ABCD variable, an exception is thrown. This is because
For variables without explicitly specifying the object chain, the JavaScript engine will try to find the variables from the latest scope. If the search fails, it will return to the parent-level function chain for search. If all searches fail
An error occurred while listing "undefined variables.

These two values are also different when performing numeric operations.

 
<ScriptType= "Text/JavaScript">
Alert (1 + undefined );// "Nan"
Alert (1 +Null);// "1"
</Script>

In the sense of null and undefined, this is well understood.

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.