Reputation: 64026
Given that in JavaScript
console.log("var F=new Boolean(false)")
console.log("( F != (F==false))",(( F != (F==false)) ? "TRUE" : "false"));
console.log("(!F != (F==false))",((!F != (F==false)) ? "TRUE" : "false"));
prints:
( F != (F==false)) TRUE
(!F != (F==false)) TRUE
which means that Boolean objects are not dop-in substitutes for a boolean primitive in typical conditions like:
if(someBoolean) ... // always true
if(!someBoolean) ... // always false
And that JavaScript's Set
and Map
collections permit any type, including primitives.
What use are Boolean objects, in particular; and the objects representing other primitive types in general, since they have all sort of weird in consistencies associated with them?
Note: I am specifically asking what are the use-cases (if any), not how they are different from their primitive counterparts.
Upvotes: 16
Views: 1663
Reputation: 5955
Say we have...
var F = new Boolean();
var f = false;
Where F is an object and f is a primitive.
Objects are passed by reference. Primitives are passed by value.
Whatever the type of an object is, it always retains its individuality; identity. It behaves like a real (material) object, taking its own unique space in the Universe.
Therefore...
var Fn = new Boolean();
is not 'equal' to F, even when comparing with dynamic type operator '==' even-though they are of the same type and carry the same value:
Fn == F;
>> false
That's because ( as already underlined ), they are two separate & different objects who carry items of the same value, ie., false. Which is nonetheless - not the same false.
And, because there's no type conversion ( since they are of the same type already ) they will be compared by reference - which are obviously pointing to two separate objects, meaning: they are not the same object!
We should imagine our JavaScript objects of a given type as packets specifically designed to carry a certain type of valuables. This way of thinking will make it far easier to understand even the most unexpected results of our work. And this is why null is beautiful.
The use case of a practical value (would have been), that a certain Boolean object can carry both values but remain the same (identifiable) object. Alas the Boolean object has been left at its initial, introduction state in JavaScript development. And is now practically useless and can only be used to determine that this false:true value comes from a different process and return than the one you are comparing it too.
Regards.
Upvotes: 4
Reputation: 288220
Boolean objects are just objects, and thus are truthy.
console.log(!!new Boolean(true)); // true
console.log(!!new Boolean(false)); // true
Boolean objects exist because this way you can add methods to Boolean.prototype
and use them on primitive booleans (which will be wrapped into booolean objects under the hood).
Boolean.prototype.foo = function() {
console.log("I'm the boolean " + this + "!!");
};
true.foo();
false.foo();
They are also useful when you want to store properties in a boolean.
var wrappedBool = new Boolean(false);
wrappedBool.foo = "bar";
console.log("Property foo: ", wrappedBool.foo); // "bar"
console.log("Unwrapped bool: ", wrappedBool.valueOf()); // false
But that's not recommended.
Upvotes: 21