Methods of judging null, undefined and nan in JS

Source: Internet
Author: User

  This article mainly introduces the method of determining null, undefined and nan in JS, and the friends who need it can refer to the following

wrote a str = "s" ++;  then appeared nan, looking for a while.   Collect data as follows to Judge:  1. Judge the undefined:  code as follows: <span style= "Font-size:small;" >var tmp = undefined;  if (typeof (tmp) = = "undefined") {  alert ("undefined"); }</span>  & nbsp Description: TypeOf returns a string of six possible: "Number", "string", "Boolean", "Object", "function", "undefined"   2. Judgment null:    The code is as follows: <span style= "Font-size:small;" >var tmp = null;  if (!tmp && typeof (TMP)!= "undefined" && tmp!=0) {  alert ("null"); } &L t;/span>    3. Judge nan:    code as follows: <span style= "Font-size:small;" >var tmp = 0/0;  if (isNaN (TMP)) {  alert ("NaN"); }</span>    Description: If NaN The result is false compared to any value (including itself), so you cannot use the = = or = = operator to determine whether a value is NaN.   Tip: the isNaN () function is commonly used to detect the results of parsefloat () and parseint () to determine whether they represent legitimate numbers. Of course, you can also use the isNaN () function to detect arithmetic errors, such as the case of dividing by 0.   4. Judge undefined and null:  code as follows: <span style= "Font-size:small;" >var tmp = undefined;  if (tmp== undefined)   {  alert ("null or undefined"); } </span>  code is as follows: <span style= "Font-size:small;" >var tmp = undefined;  if (tmp== null)   {  alert ("null or undefined"); }</span>    Description:null==undefined  <!--endfragment-->  5. Judge undefined, NULL, and nan:  code as follows: <span style= " Font-size:small; " >var tmp = null;  if (!tmp)   {  alert ("null or undefined or NaN"); }</span>    prompt: It is generally not so differentiated to use this enough.    

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.