Primitive values in javascript

Primitive values in javascript
0

#1

I was just reading YDKJS up and going. And here are something I wanna understand.

In JavaScript there are 5 primitive types: undefined, null, boolean, string and number. Everything else is an object.

Which means that the all the 5 primitive types of value are immutable. But, object is mutable.

The primitive types boolean, string and number can be wrapped by their object counterparts.

Which means that we can do something like this:

true;
Boolean(true);

But can we do something like this?

true;
Number(true);

However here is an example I found online and here are some of my questions:

typeof true; //"boolean"
typeof Boolean(true); //"boolean"
typeof new Boolean(true); //"object"
typeof (new Boolean(true)).valueOf(); //"boolean"

I don’t understand the last two line of code. What does it mean? Is new Boolean(true); a function? And the last line of the code, why it returns “boolean” ?? Could someone explain what does all these mean?


#2

Boolean is technically a function, and as such, using new Boolean(true) will give you an object.

Now that object contains (or boxes) the boolean (primitive) value true, as well as other functions, such as valueOf(), which just returns that enclosed boolean value.


#3

The new keyword creates a new object. So the typeof an object is an “object”


#4

I see . thanks! :sunglasses: