Special operators of JavaScript

Source: Internet
Author: User

First, = = =The following rules are used to determine whether two values are equal = = =: First,= = Equality equivalent,= = = Identity identity.
= =, when the value types on both sides are different, type conversion is performed before comparison.
= = =, do not type conversion, different types of certain range. 1. If the type is different, [unequal]
2, if two are numeric, and is the same value, then [equal]; Exception) is, if at least one of them is Nan, then [unequal]. (Determine if a value is Nan and can only be judged by isNaN ())
3. If two are strings, the characters are the same for each position, then [equal]; otherwise [unequal].
4, if the two values are true, or both are false, then [equal].
5. If all two values refer to the same object or function, then [equal]; otherwise [unequal].
6, if two values are null, or both are undefined, then [equal].second, = =According to the following rules: 1.If the two value types are the same, the = = = comparison.
2.If two value types are different, they may be equal. Type conversions are then compared according to the following rules: A, if one is null, and one is undefined, then [equal].
B, if one is a string, one is a numeric value, and the string is converted to a number and then compared.
C, if any value is true, convert it to "1" and then compare it to "0" if either value is false.
D, if one is an object and the other is a numeric value or a string, convert the object to "the value of the underlying type and then compare". The object is converted to the underlying type, using its ToString or ValueOf method. JS Core built-in class, will try to valueof before ToString, the exception is that date,date is using the ToString conversion. Non-JS core object, do not explain here.
E, any other combination, are [unequal]. Example:
"1" = = True Type, true will be converted to a value of 1, now become "1" = = 1, and then the "1" into 1, compare 1 = = 1, equal.

= Assignment operator
= = equals
= = = Strictly equal To
Example: var a = 3; var B = "3";

A==b returns True
A===b return False

Because the type B is different, = = = is used for strict comparative judgment

In short, "= =" requires only equal values. "= = =" Requires both values and types to be equal.

third, && operators

from the understanding of other languages, the meaning of the expression is to connect multiple logical conditions to determine the true and false, if the condition of the connection has a false, then return FALSE. In fact, JS in the && operator is also the meaning. in JS, any type of value can be converted to a logical Boolean value such as:

Non-empty string       →    true
Not 0 digits           →   true
true                →   true
  
null                 →  false
undefined        →  false
false               →  false
nan                →  false
0                    →  false


For example:
var a1 = "AA";
var a2 = "BB";
var a3 = "CC";
var AA = A1 && A2 && A3; 3 conditions are connected to determine whether true or false, both are true returns the last value.
alert (AA); Back to A3


The following parsing
(1) if 3 conditions are true, a third value is returned.
(2) if all 3 conditions are false, the first value is returned, and because of the short-circuit algorithm, the first value is false from left to right, and the second value and the third value are ignored.
(3) if only one of them is false, then the false value is returned, and the following value is ignored according to the short-circuit algorithm.

The above logic actually expresses the following meanings:
if (!A1) A1 does not make sense to return A1
alert (A1);
Else if (!A2) A2 does not make sense to return A2
alert (A2);
else if (!A3) A3 does not make sense to return A3
Alert (A3);
Else
Alert (A3); It all has a meaning. Return to the last A3

Four, | | | operator

Understanding the preceding && operator, which means that if you want to make the expression meaningful, left-to-right, as long as you find that there is a meaning, the entire expression makes sense and returns the meaningful value, and if it is meaningless, it returns the last value.

var a1 = null;
var a2 = NaN;
var a3 = undefined;
var aa = A1 | | A2 | | A3;
alert (AA); Back to "undefined"


The following parsing
(1) if 3 of these conditions are true, the first true value is returned, and the second and third values are ignored according to the short-circuit algorithm, because the first value already determines the result.

(2) if only one of them is true, return the value that is true. The following conditions are ignored.

(3) if both are false, return the last False value.
var AA = A1 && A2 && A3;
If you are using if to represent the following:
if (A1)
alert (A1);
Else
{
if (A2)
{
alert (A2);
}
Else
Alert (A3);
}

JS often used in | | Operators to make logical judgments. Such as:

var a = false;
var B = [n/a];
var aa = a | | b//Returns the second if the first value has no meaning, otherwise returns the first one

The meaning of the expression is as follows:

if (a = = null)
alert (b);
Else
alert (a);

it is equivalent to the following in C #:
string A = = null? B:a

let's distinguish the following code:
var a = undefined;
var b =[1,2,3];

var c = (a==undefined) && b//If A is not initialized, B is used instead of
var d = A | | b//If a equals false, use B instead
alert (c);//Return to
alert (d);//Return to

Finally, a summary of && and | | The difference between the operators:

(1) Both are to calculate the true and false connection conditions, and finally to determine the value, are starting from false test.
(2) for && if the first value is false then the entire expression is false and the first value is returned, and subsequent conditions are ignored.
(3) for | | If the first value is false, it continues to see if the following condition is true, returns immediately if the truth is found, and returns the last value if no truth is found at the end.

Special operators of JavaScript

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.