Distinguish undefined, null, ", 0, false, and Nan in Javascript

Source: Internet
Author: User
Tags script tag
In fact, I have already listed all the "null" and "false" values in Javascript in the title, except that the values of Boolean are true and false, the "null" values of other data types mainly include undefined and defined. The types of these null values are:

( Typeof Undefined) = 'Undefined'
( Typeof Null ) = 'Object'
( Typeof "" ) = 'String'
( Typeof 0 ) = 'Number'
( Typeof False ) = 'Boolean' ( Typeof NAN) = 'number'

The common feature of these six values is that when you make judgments in the IF statement, the false branch is executed. In a broad sense, it means that these values are invalid values or null values corresponding to their data types. There are also five values! Returns true.

These values are also different. Among them, undefined and null are special. Although the null type is object, null does not have any object features, that is, we cannot execute null. tostring (), null. default call of object instances such as constructor. So in this sense, null and undefined have the greatest similarity. Look at the result of null = undefined (true) to better illustrate this. However, there is a difference between similarity and similarity, that is, the 10 + null result is 10, and the 10 + undefined result is Nan.

In addition, "", 0, and false although the if statement is expressed as "false value", they are all meaningful data, but are treated as "null" or "false value", because :"". tostring (), (0 ). tostring () and false. tostring () is a valid executable expression.

In fact, these six values do not bring too much problems to the process control in the above-mentioned differences. So what should they be differentiated? Note that the differences between these values and string values are quite large. The conversion relationships between these values and string values are as follows:

String (undefined) ->   " Undefined "
String ( Null ) ->   " Null "
String ( "" ) ->   ""
String ( 0 ) ->   " 0 "
String ( False ) ->   " False " String (NAN)-> 'nan'


When declaring the language in the script tag, the previous version is included. The undefined values after 1.3 are null, and the previous undefined values are all undefined. Different versions have different results, and the latest versions are all classified as 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.