The title

Q: If variable a is equal to what, it can be an if statement, and the console can print ‘OK’.

vara = ? ;if (a == 1 && a == 2 && a == 3) {
    console.log('OK');
}
Copy the code

Their thinking

The operand to the right of the == == operator is numeric.

If you only compare values of type number, the condition will not be satisfied.

The == equal operator compares two operands, and if the operands have different data types, it performs a conversion before comparing them for equality.

So if a is a value that can be converted to a number, then it might make sense.

Only Number() can convert a value of any data type, according to the three functions Number(), parseInt(), and parseFloat(), which convert a value of any other data type to Number.

According to the conversion rules of Number(), a can be excluded as a primitive value type, because a primitive value cannot be equal to three values, so it can only be an object type value.

Scenario 1: Suppose a is an object

According to the rules for converting objects to numbers, redefine a function for a’s symbol.toprimitive property (method) that changes its default return value.

The specific code is as follows:

var a = {
    i: 0
};
// Redefine a function for the Symbol. ToPrimitive property of A
a[Symbol.toPrimitive] = function toPrimitive() {
    return ++this.i; // this -> a
};
if (a == 1 && a == 2 && a == 3) {
    console.log('OK');
}
Copy the code

Each time the == == comparison is performed, a cast is triggered, which calls the symbol.toprimitive property (method) and returns the value of a.i cumulative 1, so the return value of each cast is different and cumulative.

Scenario 2: Suppose a is an array

Following the rules for converting objects to numbers, we can override the toString() pointer to an array instance as follows:

var a = [1.2.3];
a.toString = a.shift;
if (a == 1 && a == 2 && a == 3) {
    console.log('OK');
}
Copy the code

Shift () removes the first element of an array and returns the deleted element.

knowledge

1. == Rules to follow when converting operand types

(1) If any of the operands is a Boolean value, it is converted to a numeric value and then compared for equality. False converts to 0 and true converts to 1.

(2) If one operand is a string and the other is a value, try to convert the string to a value and compare for equality.

(3) If one operand is an object and the other is not, call the valueOf() method of the object to get its original value and compare it according to the previous rules.

2. == Rules to follow when making comparisons

(1) Null equals undefined.

(2) Null and undefined cannot be converted to other types of values for comparison. That is, null and undefined are not equal to any other value.

(3) NaN is not equal to any value, including itself.

(4) If both operands are objects, compare whether they are the same object. That is, to compare whether they store the same heap memory address.

Associated notes

The above content belongs to the personal study notes arrangement, if there is any wrong please correct.