0

Type coercion is the implicit type conversion of an operand by an operator.

IIUC, when evaluating 'a'.length, a is temporarily wrapped, or boxed, in a instance of String, which is (?) strictly a different type to a string literal.

Is type coercion involved here?

console.log(typeof new String('') === typeof '') // false
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • new String('') is instance of String object, '' is just string – dellink Apr 23 '20 at 13:16
  • same as `new String("") === new String("")` – xdeepakv Apr 23 '20 at 13:16
  • 2
    not sure what .length has to do with typeof a String Object. You have `console.log("object" === "string")` – epascarello Apr 23 '20 at 13:16
  • `new String("").toString() === new String("").toString() // True` – xdeepakv Apr 23 '20 at 13:17
  • @epascarello the type of the string literal `'a'` does not have a property `length`. The `String` type does, however, have such a method on its prototype. Maybe this question boils down to: is a string literal a different type from its constructor-created counterpart? `typeof` suggests it is different. – Ben Aston Apr 23 '20 at 13:19
  • Well length is not a method to start. – epascarello Apr 23 '20 at 13:21
  • https://stackoverflow.com/questions/2051833/difference-between-the-javascript-string-type-and-string-object – epascarello Apr 23 '20 at 13:22
  • 3
    I think the question is, "Is auto-boxing a form of type coercion?" – Pointy Apr 23 '20 at 13:25
  • 1
    And the answer https://stackoverflow.com/a/2051862/14104 points to the rules on how the property of the string is fetched. – epascarello Apr 23 '20 at 13:32
  • 1
    Your definition of `auto-boxing` and `type coercion` are not clear. When you access a property of a javascript primitive type other than `object` then it gets `wrapped` using the appropriate wrapper for that primitive type. https://www.ecma-international.org/ecma-262/10.0/index.html#sec-getv – Robert Apr 23 '20 at 14:11
  • Thank you Robert. Happy to add clarification where necessary. – Ben Aston Apr 23 '20 at 14:14

1 Answers1

1

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.

Robert
  • 2,603
  • 26
  • 25
  • This is the only answer I could find that explains why e.g. `(2).toString()` works - because the spec says property access converts to object before trying to access a property. – G. Bach Nov 15 '20 at 13:12
  • @G.Bach Why shouldn't it work? The only reason why `2.toString()` does not work is because the parser always interprets the first dot as a decimal separator. Even `2..toString()` works. I'm sure most people never actually used String/Number/etc objects but still used their methods. That's the normal usage and the specs somehow evolved around that habit. When you try to access the properties of a primitive, you access the properties of a wrapper. – Robert Nov 16 '20 at 02:08
  • Yes, I know; but that difference between `2.toString()` and `(2).toString()` is what I couldn't figure out with the info found elsewhere. – G. Bach Nov 16 '20 at 12:24