Learn to remember: JS to judge the special value and the conversion of the Boolean type

Source: Internet
Author: User

To the jquery and some other JS framework source code, often can see the following judgment,

I have been accustomed to the C # High-level language grammar, I have not been able to systematically understand this piece of code.

var//dosomething   ... if (test) {   //dosomething  ...}

Here is a systematic record of this JS special value and the conversion of the Boolean type, the test code and results are as follows

Window.onload =function () {            varMinusnum =-1; varZero = 0; varone = 1; varStringempty = ' '; varStringnotempty = ' 111 '; varObjfunc =function () { }; Console.group ("Conversion of special values and bool in JS"); Undefined? Console.info ("Undefined is true"): Console.info ("Undefined is false"); NULL? Console.info ("Null is True"): Console.info ("Null is false"); Minusnum? Console.info ("negative is true"): Console.info ("negative number is false"); Zero? Console.info ("zero (number 0) is true"): Console.info ("zero (number 0) is false"); Stringnotempty? Console.info ("Stringnotempty is True"): Console.info ("Stringnotempty is false"); Stringempty? Console.info ("Stringempty is True"): Console.info ("Stringempty is false"); Objfunc? Console.info ("Objfunc is True"): Console.info ("Objfunc is false"); NaN? Console.info ("Nan is True"): Console.info ("Nan is false"); Console.groupend ();}

the difference between = = and = = =

Judging nature can not be separated from = =, while quoting records. Original: http://blog.sina.com.cn/s/blog_4b32835b01014iv9.html

1, for String,number and other basic types, = = and = = = There is a difference

1) comparison between different types, = = Comparison of "converted to the same type after the value" see "Value" is equal, = = = If the type is different, the result is unequal

2) Comparison of the same type, direct "value" comparison, the results of the same

2, for Array,object and other advanced types, = = and = = = There is no difference between

Make a "pointer address" comparison

3, the basic type and the advanced type, = = and = = = There is a difference

1) for = =, convert advanced to base type, and compare "value"

2) = = = = False due to different type

typeof and Instanceof

1. typeof is used to check the data type of the variable and returns a string.

typeof returns a string of 7 possible types: "Number", "string", "Boolean", "Object", "function", "undefined", "symbol",

The variable that typeof checks is an object, or Null, and it returns object.

2, instanceof is to see if a variable is an instance of an object, returns a Boolean value.

  Instanceof can only be used to determine object types such as objects and functions, and cannot be used to determine underlying types such as strings and numbers.

Learn to remember: JS to judge the special value and the conversion of the Boolean type

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.