Let’s just get started and the question is

if(a==1 && a==2 && a==3){
    alert('Cool.')}Copy the code

To be honest, anyone who writes like this in development is guaranteed to be killed.

But an interview is an interview, and there are considerations for the interviewer.

There are two points I understand

1. Implicit type conversion

Let’s start with a few cases of implicit type conversions

Let’s start with the conditional judgment ==
  • String and Number comparison: ‘1’ == 1, strings are converted to numbers by calling the Number() method and then compared.

  • Boolean and Number: true == 1, Boolean calls Number() to convert to a Number.

  • Boolean and string comparisons: true == ‘1’ converts both calls to Number() and then compares.

In summary, both sides of == will call Number() after the conversion.

Let’s talk more about the + – * / operator

These four can be divided into two categories, + and others, and we all know that + can be both an operator and a string chaining.

1 + 1 + 1 = 2"1" = "11"2 -'1'2 * = 1'1'2 - / = 2'1' = 2
Copy the code

In addition to the + operator, any operator that has a string Number on either side will also call the Number conversion.

At this point you realize that this has nothing to do with the topic! Someone from experience tells you to calm down! To look down

2, valueOf/toString and other native API master degree

For native apis like valueOf/toString for objects, we look at comparisons between numbers and objects/operations between numbers and objects

We concluded that implicit conversions alone do not solve the problem

Js is true except for empty strings, digits 0, null, undefined, false, NaN is false.

**JavaScript calls the valueOf method to convert the object to its original value. You rarely need to call the valueOf method yourself; JavaScript automatically calls it when it encounters an object with a raw value to expect. That’s what the government says.

1 == {
    valueOf:()=>{
        return1}} / /true
Copy the code

The result is that the object automatically calls the valueOf method

So we can change it to:

a = {
      value :0,
      valueOf :function(){
            this.value++
            return this.value
      }
}

1==a //true
2==a //true
3==a //true/ / for the resultstrueValueOf is automatically called during each implicit type conversion. ToString = toString = toString = toString = toString = toString = toString = toString // I can only say that the js authors are awesome. A = {value :0, valueOf:function(){
            this.value++
            return this.value+'aa'
      },
      toString:function(){
           this.value+=2
           return this.value
      }
}

Copy the code

That’s the rule. When valueOf returns a non-primitive type, go back and call toString.

I called it three times and the result is 6

There is a very SAO method, from a little sister to see the blog, juejin.cn/post/684490… Article sixth topic: I talk of this is this topic, but feel little sister chat more professional point.

letArr =[1,2,3] arr. Join = arr. Shift arr= =1 arr= =2 arr= =3 This is awesome!Copy the code