ECMAScript operators "and other exemplary operator"

Operators of other generally used in determining whether two variables are equal operation. When processing the original value, this operation is quite simple, relates to an object, a little bit complicated task.

Operators of points:

An equal sign and an equal sign for the non-treated original value

2, full and part-equal sign equals processed for

An equal sign and a non-equal (comparison value)
in ECMAScript,

1, the equal sign is represented by double equal sign (==), if and only if the two operands are equal, it returns true.

2, the non-equal sign, equals sign is represented by an exclamation mark (! =), If and only if both operands are not equal, it returns true. To determine whether the two operands are equal, the two operators are type conversion.

Perform type conversion rules are as follows:

1, if the operand is a Boolean value, before checking for equality, converts it into a digital value. false converted to 0, true 1

2, if the operand is a string, the other is a digital, before checking for equality, to try to convert the string into a digital

3, if the object is an operand, another string, before checking for equality, to try to convert an object into a string

4, if the operand is a target, the other is a digital, before checking for equality, to try to convert an object into a digital

To the point that thing chestnuts:

First, if the operand is a Boolean value, before checking for equality, converts it into a digital value. false converted to 0, true 1

console.log(false == 0);//true
console.log(true == 1);//true
console.log(false == 1);//false
console.log(true == 0);//false
console.log(false != 0);//false
console.log(true != 1);//false
console.log(false != 1);//true
console.log(true != 0);//true

Second, if the operand is a string, the other is a digital, before checking for equality, to try to convert the string into a digital

console.log("12" == 12);//true
console.log("12" != 12);//false

Third, if an operand is an object, another string, before checking for equality, to try to convert an object into a string

obj = var [ 'A']
var STR = "A"
the console.log (obj.toString ()); // A - after> converted into a string of A
the console.log (STR == obj); // to true
console.log (obj = str!); // fasle

Fourth, if an operand is an object, the other is a digital, before checking for equality, to try to convert an object into a digital

obj = var [ '12']
var the Num = 12;
the console.log (Number The (obj)); // 12 - 12 then becomes> into a digital
console.log (obj == Num); // true

In comparison, the operator also observe the following rules:

1, the value is equal to null and undefined

2, when checking for equality, not null and undefined converted into other values

3. If an operand is NaN, the equal sign will return false, non-equal sign will return true

4, if the two operands are the object, the comparator is a reference value thereof. If two operands point to the same object, then returns true equals, otherwise unequal two operands

Our good friend, comrade came chestnuts:

First, the null and undefined values ​​are equal

var a = '';
console.log(a.innerHTML);//undefined
console.log(null == a.innerHTML);//true
console.log(null != a.innerHTML);//fasle

Second, when checking for equality, not null and undefined converted into other values

This would be understood literally on the line, I do not know how to find you the example, CGL sorry for people.

Third, if an operand is NaN, the equal sign will return false, non-equal sign will return true

console.log (NaN == null); // false
console.log (NaN == undefined); // false
console.log (NaN == NaN); // false
console.log (NaN == 0); / / false
console.log (NaN! = 0); // true

Fourth, if the two operands are objects, so the comparison is their reference values. If two operands point to the same object, then returns true equals, otherwise unequal two operands

Examples of when a little cut, we will look at it probably means is this.

var fn1 = function () {console.log ( ' I Fn1')};
var = Fn1 Fn2
var Fn3 = function () {Fn1 ()} function () {// with a single: www.gendan5.com

fn1()
fn2()
fn3()
console.log(fn1 == fn2);
console.log(fn2 == fn3);

was FN4 = [1,2,3];
was FN5 = [1,2,3];
console.log (FN4 == FN5);

Second, full and part-equal sign equal sign (comparison value and type)
equals the equal sign and similar non-operator is an equal sign and part-full equal sign. These operators made with non-equal sign and an equal sign identical, except that they are before checking for equality, type conversion is not performed.

1, all equal sign is represented by three equal sign (===), only without the need to type conversion operands equal, it returns true.

2, represented by the non-full equals equals two plus exclamation (! ==), only in the type conversion without operand are not equal, it returns true.

was num1 = 30;
was num2 = 30;
was size = "30";

console.log (num1 === num2); // true -> values, the digital types are
console.log (num1 === str); // false -> values, the digital type and strings
console. log (NaN === NaN); // false -> direct false, rote
console.log (num1 = str!); // false -> not the same type, but the same value
console.log (NaN = NaN!) ; // true -> direct true, rote
console.log (NaN == NaN!); // true -> direct true, rote
console.log (num1 == num2!); // false -> value and They are the same type

Other watch "non-equal sign and equal sign" on it.

Guess you like

Origin blog.51cto.com/14513127/2440287