Non-null judgments in Javascript undefined,null, the difference between Nan

Source: Internet
Author: User

Original source: http://blog.csdn.net/oscar999/article/details/9353713

JS data type before you introduce the differences between the three, take a look at the JS data type. In a language like Java, C, before using a variable, you need to define the variable and specify its data type, Integer, String, .... But in JS, the definition of variable uniform usevar, or you can use it without using Var. Is there a concept of data type in JS? Of course there is, usingtypeofyou can determine the data type of this variable: [JavaScript] View plaincopy<!--Add by oscar999--> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta name= "Author" content= "oscar999" > <script>s= "This is Test"; Alert (typeof(s)); </script> </HEAD> <BODY> </BODY> </HTML> The above example pops up with a value of "string", it can be seen that JS is also a data type. The data types in JS are undefined,Boolean, Number,string,object and so on 5 kinds, the first 4 are the original type, the 5th kind is the reference type. What is the difference between a primitive type and a reference type? A reference to the concept of another language is similar to a reference, which is an address. Take a look at this example to find out. [JavaScript] View plaincopy<!--Add by oscar999--> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta name= "Author" content= "oscar999" > <script>varobj =NewObject (); varObjcopy =obj; OBJ.ATT1= "obj attribute";  alert (OBJCOPY.ATT1); </script> </HEAD> <BODY> </BODY> </HTML>don't overlook the nature of the object type. Oh, this is where it's often misused. Changes like the above obj have caused a change in objcopy. In addition to the above 5 types, there is also a "function"type. [JavaScript] View plaincopy<!--Add by oscar999--> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta name= "Author" content= "oscar999" > <script>functionTest () {alert ("Hello"); } alert (typeof(test)); </script> </HEAD> <BODY> </BODY> </HTML>Undefined andNULL, the difference between NaN is described above, it is easy to distinguish undefined from the other two. Undefined determines the type of the variable, while the other two judgments are the value of the variable. Undefined can be used to indicate the following conditions1. Represents an undeclared variable,2. A variable that has been declared but not assigned a value.3. An object property that does not existNULLis a special object that represents no value; Nan is a special number that represents no value;= = or = = =) using==, if the types on both sides are different, the JS engine will first turn them into the same type in the comparison of the values;===, type conversions are not performed, and the types are different and certainly not equal. Examples have the above knowledge, and then look at some of the following interesting but confusing examples should be very clear: [JavaScript] View plaincopy<!--Add by oscar999--> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta name= "Author" content= "oscar999" > <script>vars; Alert (S==undefined);//truealert (s===undefined);//trueAlert (S==NULL);//trueAlert (s===NULL);//falseAlert (NULL==undefined);//trueAlertNULL===undefined);//false</script> </HEAD> <BODY> </BODY> </HTML>change var s tovars =NULLLook at the effect again ~ ~in general, JS a variable s to empty the habit of usingif(s!=NULL), if s is not defined, it will be reported undefined JS error, so the complete empty sentence can be used as follows: [JavaScript] View plaincopyif(typeof(s)! = "undefined" &&s!=NULL)

Non-null judgments in Javascript undefined,null, the difference between Nan

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.