Undefined, Nan, null in JS

Source: Internet
Author: User

Because the system does not fully learn JS knowledge, you may encounter some problems during actual development. For example, sometimes the data obtained from the database is displayed on the page with characters such as undefined and Nan, I have never known what is going on here. I checked the materials and sorted them out. It's a makeup.

Type Analysis

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. You can use typeof
Function judgment

VaR A1;

VaR a2 = true;

VaR a3 = 1;

VaR A4 = "hello ";

VaR A5 = new object ();

VaR A6 = NULL;

VaR A7 = Nan;

VaR A8 = undefined;

Alert (typeof A); // display "undefined"

Alert (typeof A1); // display "undefined"

Alert (typeof A2); // displays "Boolean"

Alert (typeof A3); // display "Number"

Alert (typeof A4); // display "string"

Alert (typeof A5); // displays "object"

Alert (typeof A6); // displays "object"

Alert (typeof A7); // display "Number"

Alert (typeof A8); // display "undefined"
From the code above, we can see that undefined is undefined for undefined values and definitions. null is a special object and Nan is a special number.


Comparison

VaR A1; // The A1 value is undefined.

VaR a2 = NULL;

VaR a3 = Nan;
Alert (a1 = a2); // display "true"

Alert (A1! = A2); // display "false"
Alert (a1 = A3); // display "false"

Alert (A1! = A3); // display "true"
Alert (A2 = A3); // display "false"

Alert (A2! = A3); // display "true"
Alert (A3 = A3); // display "false"

Alert (A3! = A3); // display "true"
From the code above, we can draw a conclusion: (1) undefined is equal to NULL; (2) Nan is not equal to any value and is not equal to itself.

Typeof returns a string, which has six possibilities: "Number", "string", "Boolean", "object", "function", and "undefined". undefined
An undeclared variable, a declared variable without a value assignment, or an object attribute that does not exist.


Differences between undefined and null in JS

In JavaScript, two values are used to represent concepts similar to null values, undefined and null, which are easily obfuscated and represent two different concepts.

Let's talk about undefined:

The variables in JavaScript are of a weak type (I don't need to explain more about this), so you only need to use the VaR keyword to declare the variables. If a strong-type language like C is used to declare a variable without an initial value, a default value is given to it. For example, the default value of the int variable is 0. However, in a weak language such as JavaScript, there is no way to determine the default value for such a variable. For example, I declare a variable.

var v1;

Is it false, 0, or ''?

It cannot be determined because there is no type. In JavaScript, if no initial value is specified for such a declaration, an undefined variable is provided. However, the premise is that the variable must have been declared. If there is no declared identifier, an error will occur. Take a look at the following code.

1 var V1; 2 alert (V1); // undefined3 alert (V2); // Error

Let's talk about null. javscript has several basic types: Number, String, Boolean, and object. There are two conditions for an object-type variable: one is an object instance and the other is an empty reference null, users familiar with object-oriented languages like Java should be easy to understand. In both cases, both types are objects. The type of a variable in Javascript is determined only when it is assigned a value, for example, the following.

 1     var v1 = 1; 2     var v2 = true; 3  4     alert(typeof v1);   //number 5     alert(typeof v2);   //boolean 6  7     v2 = new Date(); 8     alert(typeof v2);   //object 9 10     v2 = "str";11     alert(typeof v2);   //string12     13     v2 = null;14     alert(typeof v2);   //object

It can be seen that null represents a special object type value in Javascript, which is used to represent the concept of null reference. If you want to declare an identifier as an object type, however, if the instance is not provided for the time being, it can be initialized to NULL for later use.


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.