Why false == “false” is false?

前端 未结 4 1193
一个人的身影
一个人的身影 2021-02-15 14:24

I am still learning the basics of javaScript and I don\'t understand why this happens.

Having type coercion false == \"false\"would be converted into:

4条回答
  •  無奈伤痛
    2021-02-15 15:14

    The answer is because "false" is a string (as Gerardo Furado pointed out in the comments), the test you are making is equivalent to false = "hello".

    Javascript does not look at the word in the string to determine if it is a boolean and then try to get the value from that.

    Note:

    In general in javascript it is now preferred that you use the === operator, to avoid all of this.

提交回复
热议问题