0
let valueOne = 3;
let valueTwo = "3";
if(valueOne==valueTwo)
    {console.log("same")}
else
    {console.log("different")}

    console.log("valueOne==valueTwo: " + valueOne==valueTwo);

Why is the output:

same
false

It doesn't even print the string valueOne==valueTwo:. However, when extra parentheses are added it's outputting the expected results.

console.log("valueOne==valueTwo: " + (valueOne==valueTwo));   

Output:

same
valueOne==valueTwo: true

It appears that the == operator has lower precedence than concatenation in javascript, http://www.scriptingmaster.com/javascript/operator-precedence.asp which is different from Java: https://introcs.cs.princeton.edu/java/11precedence/

Anyone know the design concern here?

Thanks!

P L
  • 1
  • 1
  • JavaScript and Java are two totally different languages, the [operator precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence) is not the same in all languages. – Teemu Feb 14 '20 at 21:55
  • what's the actual question? You've already correctly explained why the code didn't behave as you expected, as well as how to fix it. – Robin Zigmond Feb 14 '20 at 22:02
  • @RobinZigmond "_Anyone know the design concern here?_" – Teemu Feb 14 '20 at 22:03
  • @RobinZigmond They want to know, why the operator prcedence in JS is designed the way it is. – Teemu Feb 14 '20 at 22:04
  • yes, sorry - I'd missed the comparison to Java. It's an interesting question I guess, but not one I can answer or one I expect there to be a satisfying answer to at all. – Robin Zigmond Feb 14 '20 at 22:05
  • Actually, on quick reflection I *can* see a very good reason. In JS it's relatively common to do something like `if (a + b == 3)`, which you would certainly want parsed as `if ((a + b) == 3)`. The other way doesn't really make sense. I'm not sure how it works in Java (a language I don't use), but I expect that the fact it's statically and strongly typed means that it would still parse it the correct way given that `if (a + (b == 3))` would be rejected anyway, whereas JS would be fine with that interpretation even though it's pretty nonsensical. – Robin Zigmond Feb 14 '20 at 22:07
  • 3
    Actually, having checked the link to the Java operator precedence given in the OP, it seems that `+` is higher than `==` in Java too, which means the question is based on a misunderstanding. – Robin Zigmond Feb 14 '20 at 22:10
  • Yeah, that's right. The two pages used different order of numbers to mark the precedence of operators. The + concatenator has the same precedence with mathematical + and - operators. I wonder how the compiler distinguish the concatenator from plus. – P L Feb 14 '20 at 22:38
  • @PL - They have the same precedence, so the parser doesn't need to distinguish them. In Java, the later phases distinguish `+` addition and concatenation based on the static types of the operand expressions. If both are numeric types, the operation is addition. If one or more is a `String` then it is concatenation. (In Javascript, I think the decision must be made at runtime based on the actual types.) – Stephen C Oct 27 '21 at 12:28
  • P.S. Since your question is based on a false premise / an incorrect reading of the respective references, you should delete it. It is no help to anyone. – Stephen C Oct 27 '21 at 12:31

1 Answers1

-1

Its adding the string and number together and comparing it to the second variable. Using a string template fixes this.

let valueOne = 3;
let valueTwo = "3";
if(valueOne==valueTwo)
    {console.log("same")}
else
    {console.log("different")}
 
    console.log(`valueOne==valueTwo: ${valueOne == valueTwo}`);