Search code examples
javascriptjavascript-objects

Javascript expression evaluation - confusion


I have an expression like below

console.log('isSuperAdmin ' + user.user_role === "SUPER_ADMIN");

which is printing false on console. But, clearly the value of user.user_role is indeed "SUPER_ADMIN" and on DevTool, when I hover on the expression user.user_role === "SUPER_ADMIN", I am getting a popup as true. Can you please tell me, why the expression is getting evaluated as false at the end?

PS: I am new to Javascript, so please point me to the right direction.


Solution

  • The + operation is done before the === so the easiest solution is to surround it with (), but it could be even better if you add the second statement as another parameter to the console.log(), so your debug window can show it in an appropiate way.

    console.log("text" + 1 === 1);
    console.log("text" + (1 === 1));
    console.log("text", 1 === 1);