Check out a free preview of the full Deep JavaScript Foundations, v3 course
The "Boxing" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:
Kyle introduces boxing and gives an example.
Transcript from the "Boxing" Lesson
[00:00:00]
>> Kyle Simpson: I referenced a bit earlier a sort of magical behavior like accessing properties on primitive values. Remember, you access a length on a primitive string or some method on a primitive number, for example. So how does that work? How is it that we access the .length of some string value here?
[00:00:23]
This DOM elements value is always a string. So how do we access that .length? Well, it turns out that's called boxing. It's a form of implicit coercion. It's not called out in the same way in the abstract operations. But I think it absolutely in spirit is an implicit coercion.
[00:00:41]
It is saying you have this thing that is not an object and you're trying to use it as if it is an object. Me, JavaScript I'm gonna be helpful and go ahead and make it into an object for you. The only other option would be for the JavaScript to throw an exception that said you're trying to access a property on an primitive value.
[00:01:01]
I am actually quite happy that JavaScript implicitly coerces these primitives into their object counterpart so that I can access properties and methods on them. I think that makes the code much more clear. If I wanted to write code where I had to turn a primitive into an object just to use it, I'd go write Java.
[00:01:19]
This, I think, is one of the unsung heroes of JavaScript, that it does this boxing and it does it well. But it is an implicit coercion. Yes?
>> Speaker 2: Is that where this notion of everything is an object comes from?
>> Kyle Simpson: It is, that's exactly what I meant when I was talking in the intro, that people think that everything's an object.
[00:01:36]
And it turns out that things can behave as objects, but that doesn't make them an object. This is not an object. It is a primitive string that has an optimization in it where you can access a property as if it was an object. So these are all examples of coercion that are happening in your programs.
[00:01:52]
And the larger point that I want to make is whether we call them coercion or we call them conversion every single language in existence that you've ever programmed in has to deal with type conversions. It is complete nonsense to suggest that you can write valid reasonable JavaScript programs and not need to deal with type conversion.
[00:02:10]
You're always gonna have a string that you need to treat as a number or a number that you need to treat as a Boolean. You are going to have to deal with conversions, aka coercion.
>> Kyle Simpson: It's absolutely necessary, so, you're gonna use it whether you're admitting it or not.
[00:02:27]
Cuz you really have no choice.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops