JTeam
JTeam

Reputation: 1505

Why boolean value is singleton in nature [JavaScript]

var bool1 = false
var bool2 = false

bool1 === bool2 

The last statement returns true, which means bool1 & bool2 points to same object instance, I want to understand why is that case?

Upvotes: 1

Views: 357

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386883

boolean is a primitive data type, not an object. The strict comparison, performs first a check if the type is the same and then the value.

Two Boolean operands are strictly equal if both are true or both are false.

If you take the an object instance of Boolean,

The Boolean object is an object wrapper for a boolean value.

you get false with strict equality.

var bool1 = new Boolean(false),
    bool2 = new Boolean(false);

console.log(bool1 === bool2);

Upvotes: 5

atiq1589
atiq1589

Reputation: 2327

triple equal ( === ) matches value and type. as both are primitive type it returns true.

And if bool1 & bool2 same instance object then changing one value will change another but it will definitely not change other value when you change either bool1 or bool.

Upvotes: 0

Related Questions