using Object name in if statement both true and false?

153 Views Asked by At

I try to use my object name for an if statement.. but both come up as true, why?

var moduleInfo = new Object("moduleInfo");
moduleInfo ["name"] = "Module: Export"

if (moduleInfo !== "moduleInfo"){
    console.log("window is NOT modulInfo")
    }


if (moduleInfo == "moduleInfo"){
    console.log("window IS modulInfo")
    }
1

There are 1 best solutions below

0
On

The !== is comparing by type, and you are comparing an object with a primitive type of string. replacing either that operator with != or replacing the second one with === will probably get you a more consistent/desired result.

== converts the operands to the same type before making the comparison https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators