In Javascript, == “” evaluates to true. Why is it so?

后端 未结 8 1580
野性不改
野性不改 2020-12-13 22:18

If I do 0 == \"0\" it evaluates to true. Try,

if( -777 == \"-777\" ) alert(\"same\");

alert happens.

And, it\'s also noticeable th

相关标签:
8条回答
  • 2020-12-13 22:22

    Javascript is a loosely typed language, so typecasts are done at runtime whenever the interpreter feels it is needed. If you compare an integer to a string, it figures that they should be the same type, so, for example, "34" == 34 is true, since the integer would probably be typecasted to a string before the comparison.

    The string "false" isn't typecasted into a bool, instead the bool false is typecasted to a string, which would acctually have the value "0", that is, a string containing the number 0, giving "0" == "false", which is obviously false.

    If you want to compare the value without automatic typecasting, effectively comparing types as well as values, use a triple equal:

    5 === "5" false "string" === "string" true

    0 讨论(0)
  • 2020-12-13 22:22

    JavaScript defines falsey values to be 0, boolean false and undefined. Any string outside of "0" will be true, even if that string is 'false'.

    Kind of annoying, really.

    0 讨论(0)
  • 2020-12-13 22:24

    Doing this:

    if(5 == "5")
    

    Makes javascript convert the first 5 to a string. Try this:

    if(5 === "5")
    

    The === makes Javascript evaluate type as well.

    This is actually a duplicate of this question where it is explained very well.

    0 讨论(0)
  • 2020-12-13 22:25

    Javascript doesn't cast "false" to boolean false, only to the string "false".

    You can loosely cast string values to their integer equivalent, thus your first example works.

    0 讨论(0)
  • 2020-12-13 22:32

    Why is it so?

    Because JavaScript is both loosely-typed, and wildly inconsistent. Not all its design features are well-thought-through; it was created, implemented and deployed incredibly quickly compared to any other programming language, in a rush to get Netscape 2.0 out. It wasn't until long after that it settled down, lost some of the more egregious bugs and became semi-standardised.

    Looking for some kind of philosophical rationale for things like the implicit type casting rules is likely to be a fruitless exercise. The only really consistent principle JavaScript adheres to is DWIM, very much in the negative sense.

    0 讨论(0)
  • 2020-12-13 22:34

    Because Javascript is loosely typed, it will silently cast your variables depending on the operation and the type of the other variables in the operation.

    alert ("5" - 1);  // 4  (int)
    alert ("5" + 1);  // "51" (string) "+" is a string operator, too
    alert ("5" == 5); // true
    

    What you might want to look at is identity checking (===). That makes sure the variables are identical, not merely equal.

    alert("5" == 5);  // true, these are equal
    alert("5" === 5); // false, these are not identical.
    

    Also see this question: How do the equality and identity comparison operators differ? PHP's implementation is very similar to javascript's.

    0 讨论(0)
提交回复
热议问题