JavaScript relational operators

Assignment operator: to be assigned to the variable

The most common assignment operator is an equal sign (=), the expression x = y to x and y represent the

= Y + X  // is equivalent to x = x + y

There are other more complex assignment operator

X - Y = // equivalent to x = x - Y 
X = Y * // equal to Y * x = x 
X / Y = // equivalent to x = x / Y 
X % = Y // equivalent to x = x Y% 
X = Y >> // equivalent to X >> Y = X 
X = Y << // equivalent to X << Y = X 
X = Y >>> // equivalent >>> X = Y X 
X Y = & // equivalent to X & Y = X 
X | Y = // equal to X = X | Y 
X = Y ^ // equivalent to x = x ^ y

These are in line with the assignment operator applies the specified operation, and then assign a value to the left

Examples :( seeking remainder of 10 divided by 3)

 

Comparison operators:

Determining a size comparison of two values, and then returns a Boolean value indicating whether the comparison condition is satisfied

Comparison 2 is greater than 1, the return value is true

A total of eight comparison operators

 Comparison operators algorithm

A data comparator can compare the value of various types, not just the value of the type of

In addition to precise equality operators and operators, comparison operators other general algorithm is as follows:

 If two operators are strings, the lexicographically comparison (Comparative Unicode coding sequence)

Otherwise, two operators are first converted into values ​​then compared. (Equivalent to first call the Number function)

Comparison of (a) a primitive type of

And the Boolean string are first converted into digital type, then compared

var aa=5 > '4' ;
console.log(aa);
// 5 > Number('4')
// 即 5 > 4
aa=true > false; console.log(aa); // Number(true) > Number(false) // 即 1 > 0
aa=2 > true ; console.log(aa); // 2 > Number(true) // 即 2 > 1

Compare (two) objects

In the pair () method to compare conversion as before, first with valueOf object to the value of the original digital type, or if the results returned by the object, you continue to call toString () method to convert it into a character type.

1, for comparison with the original data type

2, the object with the object

var AA = [2 ]; 
window.alert (AA > [. 3 ]) 

// equivalent to [2] .valueOf () toString ()> [. 11] .valueOf () toString ().. 
// i.e., '2' > '3'

 

 

 

 

Comparison (C) of the string

Compare strings lexicographically

Internal JavaScript engine will first compare the first character in the Unicode code point, if the first character of the same, it would be more the second character, and so on.

Lowercase c, Unicode code point (99) is greater than the capital Unicode code point C
(67), it returns true.

Because the Unicode character set covers all the characters, so the characters can be compared.

Because the "days" Unicode code point is 5929 , "ground" Unicode encoding is 5730 , it returns true

 

 

Identity operator (strict equality operator)

There are two equal JavaScript operators, equality operators and strict equality operators == ===

Difference: equality comparison operator == are two values ​​are equal;

Strict equality operators === comparison whether they are "the same value", if not the same type of value, strict equality operator returns false directly; and equality operators will convert the values ​​of the same data type, and then strict equality operators compared.

Strict equality operator algorithm *:

(A) a different type of value

If the value of two different types, direct return flase

Type Number 1 is "1" is a string type, for the ===, different types of data is returned flase;

Boolean is true, "true" string type, different data types return false;

First calculation is equal to 1 and the character numbers "1" to the same type, then compared, it returns true;

 

 (B) the same type of original value

The same type of a primitive type (string, Boolean, digital) are compared, the same value returns true, the value is not the same returns false

1 and Comparison 1 decimal, hexadecimal types and values ​​are the same, it returns true

 

 Note: NaN with any rank is not the same (including itself), 0 points nor negative, positive and negative 0 0 same.

A composite value (c) of the same class

Comparison, compare their values ​​are not the same as comparing the same type of match type value (objects, arrays, functions) of data, but more points to the same object

 Comparison of two dummy word objects, empty array, null function, not equal results.

Because the values ​​for the composite type, strictly equal comparison operation is the same whether a memory reference address, and empty target operators on both sides of an empty array, they empty function values ​​are stored in different memory address, the result is of course false .

If the two variables refer to the same object, they are equal, the result returns true

 

 For comparison of two objects, strictly equal comparison operator is an address, is greater than the comparison value is smaller than the number.

The first two values ​​are compared, a comparison is the last address.

new Date() > new Date() // false
new Date() < new Date() // false
new Date() === new Date() // false

 

(Iv) Undefined and null

Undefined and null and itself strictly equal

The default value of a variable declaration is Undefined, and therefore the value of the variable declaration unassigned only two are equal

 

 (E) no strict equality operator

Strict equality operators have a corresponding "strict inequality operator" (! ==), the operation result of both the opposite

 

 

Equality operators

Equality operator when the same data type, and the same operation rules strictly operator

Data types are not the same when the first character will be converted into the same type are then compared using rigorous operator.

Type conversion rules:

(A) a primitive type value

Primitive type value is converted to a value and then compare the data type

(B) the type of the object compared with the original value

Object (broadly referred to herein as objects, including arrays and functions) values ​​when compared to the original type, original object into a value type, then compared.

(C) Undefined and null type comparison

When compared with other types of data Undefined and null is returned flase, when two mutually comparing the data returns a true value

Disadvantage (iv) equalities

Sometimes the equality operator in the operator data type conversion expression changes, error-prone, it is best not to use the equality operator (==), use a strictly equal

(E) calculating unequal

Instead operator operation is not equal (! =), And equalities

 

Guess you like

Origin www.cnblogs.com/nyw1983/p/11563290.html