"Front-end" javascript determines undefined, null, NaN, string inclusions, and so on

Source: Internet
Author: User

The method of judging null, undefined and nan in JS

This article mainly introduces the method of judging null, undefined and nan in JS, the need of friends can refer to the following. Wrote a str.="s"++and then Nan, looking for a while. The information collected is as follows:1. Judge undefined: Copy the Code code as follows:<span style="Font-size:small;">varTMP =undefined;if(typeof(tmp) = ="undefined") {alert ("undefined"); }</span>Description:typeofThere are six possible types of strings that are returned:" Number"、"string"、"Boolean"、"Object"、"function"、"undefined" 2. Determine null: Copy the code code as follows:<span style="Font-size:small;">varTMP =NULL; if(!tmp &&typeof(TMP)! ="undefined"&& tmp!=0) {alert ("NULL"); }</span>3. Determine the nan: Copy the Code code as follows:<span style="Font-size:small;">varTMP =0/0; if(IsNaN (TMP)) {alert ("NaN"); }</span>Note: If you compare NaN to any value (including its own), the result will befalse, so to determine if a value is NaN, you cannot use = = or = = =operator. Tip: the IsNaN () function is often used to detect the results of parsefloat () and parseint () to determine whether they represent a valid number. Of course, you can also use the IsNaN () function to detect arithmetic errors, such as0the case of divisor. 4. Determine undefined and null: Copy code code as follows:<span style="Font-size:small;">varTMP =undefined;if(tmp==undefined) {Alert ("null or undefined"); } </span>Copy the code code as follows:<span style="Font-size:small;">varTMP =undefined;if(tmp==NULL) {alert ("null or undefined"); }</span>Description:NULL==undefined<!--endfragment-->5. Determine undefined, null, and Nan: The copy Code code is as follows:<span style="Font-size:small;">varTMP =NULL; if(!tmp) {Alert ("null or undefined or NaN"); }</span>tip: It is often enough to use this less differentiated. 

Judgment string contains, ignoring case:

Change them all to uppercase or lowercase, and then compare them. The  character position of the substring within the first occurrence of the IndexOf object  with toLowerCase or touppercase  

Resources:

Http://www.jb51.net/article/48481.htm

Http://www.cnblogs.com/binaryworms/archive/2010/04/08/1707064.html

Http://www.cnblogs.com/mingforyou/archive/2013/03/04/2942453.html

http://rock541.iteye.com/blog/1707764

Http://www.jb51.net/article/15445.htm

Http://www.ijavascript.cn/jiaocheng/javascript-indexof-244.html

"Front-end" javascript determines undefined, null, NaN, string inclusions, and so on

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.