DEV Community

loading...
Cover image for Do you know what πŸ“¦ Autoboxing in JS is?

Do you know what πŸ“¦ Autoboxing in JS is?

benjaminmock profile image Benjamin Mock Originally published at codesnacks.net ・1 min read

Let's start with the question of "What are primitive types, and how are they defined?".

Primitive types don't have methods or properties on them.

Let's see some primitive types in JS. Let's try a number and a string.

const name = "Doggo"
const age = 7

console.log(typeof name) // string
console.log(typeof age) // number

name has the primitive type string, age is a number. Both of these primitive types should not have any properties or methods on them. Let's check that:

console.log(name.length) // 5
console.log(age.toString()) // "7"

Why does this work and not throw an error? It looks like both of the primitive types are actually objects! But they're not! They just behave like objects because of autoboxing. Whenever we try to access a method or property on a primitive, that primitive is wrapped into an object. That's called autoboxing. Autoboxing will connect the primitive to the related built-in prototype object. In our case that's String.prototype and Number.prototype. This gives us access to the prototype methods and properties.

This, for example, is the number prototype:

Number prototype


Want to get better at Web Development?
πŸš€πŸš€πŸš€subscribe to the Tutorial Tuesday βœ‰οΈnewsletter

Discussion (1)

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

Why can't Java autoboxing just like JavaScript?

Forem Open with the Forem app