Javascript boolean data and = Operator.

Source: Internet
Author: User
Tags true true

Today, I saw an article in blue.ArticleIn JavaScript, the Boolean and = operators are compared and analyzed.

I don't know. I was shocked. I didn't notice such details at ordinary times:

It is estimated that this is one of the important differences between Daniel and cainiao.

When it comes to the boolean type, it is estimated that many people know that the non-zero value in JS is true, so please run the following two lines in the pipelineCode:

1 VaR Itest = 2 ;
2 If (Itest)
3 {
4 Alert ( True );
5 }
6 If (Itest = True )
7 {
8 Alert ( True );
9 }

 

What do you think is the answer?

The answer is: True true

---- Sorry, the answer is true or false, but I believe that the answer is the first one in the eyes of many people.

In order for everyone to remember or know something that is easy to ignore, I will record these differences or details:

1. Simply run a Boolean value. True is true if it is not 0, for example:
 

1 VaR I1 = 1 ;
2 VaR I2 =- 1 ;
3 VaR I3 = 0 ;
4 Alert (Boolean (I1 )); // True
5 Alert (Boolean (I2 )); // True
6 Alert (Boolean (I3 )); // False
7 If (I1 ){} // True
8 If (I2 ){} // True
9 If (I3 ){} // False

 

2. The = operator is not simply to convert non-0 values to true, and vice versa, rather:

1,If type (X) is Boolean, return the result of the comparison tonumber (x) = y.
If type (Y) is Boolean, return the result of the comparison X = tonumber (y ).

That is to say, if an operand is of the bool type, it will be converted to a number for comparison, And the rule is: True-> 1 false-> 0

For example:

1 VaR I1 =- 2 ;
2 VaR I4 = 2 ;
3 VaR I2 = 1 ;
4 VaR I3 = 0 ;
5 If (I1 = True ){} // True-> 1, so 1! =-2 false
6 If (I2 = True ){} // True-> 1, so 1 = 1 true
7 If (I3 = True ){} // This is obvious. False
8 If (I4 = True ) {] // True-> 1, SO 2! = 1 false
9 // // However, the following is different:
10 If (I1 ){} // Equivalent to boolean (-2), not 0, so true
11 If (I2 ){} // It is not 0, so it is true
12 If (I3 ){} // 0, so false
13 If (I4 ){} // Not 0, so true

 

2. If type (X) is number and type (Y) is string, return the result of the comparison X = tonumber (y ).
If type (X) is string and type (Y) is number, return the result of the comparison tonumber (x) = y.

That is to say, when one operand is a string and the other is a number, the string is converted to a number, for example:

1 VaR A =   " 1 " ;
2 VaR B =   " 001 " ;
3 VaR C =   "" ;
4
5 If ( = 1 ){} // A => Number (a) True
6 If (B = 1 ){} // B => Number (B) True
7 If (C = 1 ){} // B => 0 false

 

3. non-null strings are converted to true (whereas null and undefined are false), for example:

1 VaR A = " A " ;
2 VaR B = "" ;
3 If (){} // True
4 If (B ){} // False

 

 

------

Here, we think of jquery's $. When this item selects an element, the result returns a jquery object regardless of whether the element exists.

Therefore, you cannot use if ($ ('# id') to determine whether the image is selected. You can use $ ('# id'). length> 0 to judge.

Original article: http://www.blueidea.com/tech/web/2010/7576.asp

------

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.