1

When you use the . operator on a variable that is a primitive data type, the variable will be autoboxed, for example:

var str = "Hello World";  // str is of type string

console.log(str.length);  // a temporary object of type String will be created

But does autoboxing only happen when the . operator is used?

Steven
  • 143
  • 4
  • Does this answer your question? [Does javascript autobox?](https://stackoverflow.com/questions/17216847/does-javascript-autobox) – David Oct 06 '21 at 15:26
  • have a read of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf – Lawrence Cherone Oct 06 '21 at 15:31

1 Answers1

0

No, it's used whenever you access a property on a primitive, which has a corresponding constructor.

E.g.

'abc'[2] // this will access 'c'

As you can see, no '.' accesor was used.

d.k
  • 4,234
  • 2
  • 26
  • 38