The difference between undefined, null, and NaN in Javascript

Source: Internet
Author: User
Before introducing the differences between the three JS data types, let's take a look at the JS data types. In a language such as Java and C, before using a variable, you must first define the variable and specify its data type, which is an integer, string type ,.... but defining the variable system in js... syntaxHighlighter. all (); before introducing the differences between the three JS data types, let's take a look at the JS data types. In a language such as Java and C, before using a variable, you must first define the variable and specify its data type, which is an integer, string type ,.... however, you can define variables in js to use var in a uniform way, or you can also use it without using var. Is there a data type concept in js? Of course, you can use typeof to determine the Data Type of this variable: [javascript] New Document Script s = "This is Test"; alert (typeof (s); script New Document Script s = "This is Test"; alert (typeof (s); script  The value popped up in the above example is "string". It can be seen that js also has a data type. The data types in js include undefined, boolean, number, string, and object. The first four are original data types and the second are reference data types. What is the difference between the original type and the reference type? References are similar to references in other languages. Let's take a look at this example. [Javascript] New Document Script var obj = new Object (); var objCopy = obj; obj. att1 = "obj attribute"; alert (objCopy. att1); script New Document Script var obj = new Object (); var objCopy = obj; obj. att1 = "obj attribute"; alert (objCopy. att1); script  Don't ignore this feature of the object type. This is a frequent misuse. Similar to the above obj changes, the objCopy changes. In addition to the above five types, there is also a "function" type. [Javascript] New Document Script function test () {alert ("hello");} alert (typeof (test); script New Document Script function test () {alert ("hello");} alert (typeof (test); script  The difference between undefined and null and NaN can be easily distinguished from the other two. Undefined determines the type of the variable, while the other two determine the value of the variable. Undefined can be used to indicate the following conditions 1. represents an undeclared variable, 2. variables declared but not assigned values, 3. null is a special object that indicates no value; NaN is a special number that indicates no value; comparison operator (==or ==) use =. If the types on both sides are different, the js engine first converts them to the same type for comparison of values; Use =, then the type conversion is not performed, and the types are different, certainly not equal. When the instance has the above knowledge, let's look at some interesting but confusing examples below: [javascript] New Document Script var s; alert (s = undefined); // true alert (s = null ); // true alert (s = null); // false alert (null = undefined); // true alert (null = undefined ); // false script New Document Script var s; alert (s = undefined); // true alert (s = null ); // true alert (s = null); // false alert (null = undefined); // true alert (null = undefined ); // false script  Change var s to var s = null and check the effect ~~ Generally, if (s! = Null). if s is not defined, the undefined js error will be reported. Therefore, you can use the following method to complete null determination: [javascript] if (typeof (s )! = "Undefined" & s! = Null) if (typeof (s )! = "Undefined" & s! = Null)
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.