Is auto-boxing a form of type coercion?
Your definition of auto-boxing
and type coercion
are not clear. As far as I know auto-boxing
isn't a Javascript concept.
On MDN type coercion
is defined as "the automatic or implicit conversion of values from one data type to another".
Usually the term is only used in the context of operators other than property access. But when you access a property of a javascript primitive type other than object
or undefined
then it gets wrapped using the appropriate wrapper for that primitive type. And this operation involves type coercion
.
In non-strict mode the wrapping happens again when you actually call the function to produce a this
of object type, while in strict mode this
is of the original type.
Does console.log(typeof new String('') === typeof '')
involve type coercion?
No. There is no coercion involved:
new String('')
produces a string object.
typeof aStringObject
produces the string primitive "object"
.
typeof ''
produces the string primitive "string"
.
"object" === "string"
produces the boolean primitive false
.
console.log
can only output strings, but Javascript function parameters have no types. The implementation of console.log(false)
probably uses type coercion
or explicit type conversion to convert the boolean value false
to the string value "false"
though.