This is the fifth post in a series I'm writing as I learn JavaScript.
By now this series has a running theme: I learn a rule, then I find something that seems to break it, and chasing down why teaches me more than the rule did.
Here's this one.
I'd just learned that strings are primitives . Not objects β primitives. A string is a single value, like a number or a boolean. Fine.
Then, on the same day, I wrote this:
let name = " parth " ; console . log ( name . toUpperCase ()); // "PARTH" console . log ( name . length ); // 5
Enter fullscreen mode
Exit fullscreen mode
And stopped.
.toUpperCase() is a method . .length is a property . Those belong to objects . But I was just told a string is a primitive, not an object. So how does a primitive have methods hanging off it?
Either strings are secretly objects, or something sneaky is happening. It turns out to be the second one, and it has a name: autoboxing .
First, strings really are primitives
Just to be sure I hadn't misunderstood:
console .β¦