ES6: The Right Parts When to use Const
Transcript from the "When to use Const" Lesson
>> Speaker 1: The question was about using const for immutable values like strings and numbers. I'm not 100% sure what the questions actually asking. It's Mark A. About three minutes ago.
>> Kyle Simpson: Okay I think the question is where am I going to use const? Place where I think you should use const is where you were already doing stuff like this.
[00:00:29] If you did something silly like var pi equals 3.14. You have my blessing to go change those all to consts because those really are. Mutable values that you don't intend to reassign and if you want to put const on it that's no big deal. But I don't think you ought to go do like const f equals arrow function or anything like that.
[00:00:49] Because I think you're just inviting additional visual clutter that's not actually communicating what you think it's communicating. Okay yes?
>> Speaker 3: What's your take on using const for function name? Like let's say you create a utility function then you're calling it add some or something.
>> Kyle Simpson: Like I just said, I would not do const add some equals and then some function.
[00:01:11] I just make a function declaration.
>> Kyle Simpson: So there are relatively few places in my code where I have actual constants like this. But in those places I'd endorse using the const. And I don't think you should lead off every one of your declarations in your code with a const.
[00:01:32] Because I think you're inviting unfamiliar readers with your code to potentially have some visual stumbling block.
>> Speaker 1: Is there any benefit of using const on a raise then, or objects?
>> Kyle Simpson: I don't think so at all. Matter of fact, this is a little controversial because I've had debates with people about this before.
[00:01:55] But saying something like const x equals array one, two, three. Let's say that this was a really huge array like it was 100 megabytes of memory. And let's say that it was inside of some kind of function foo. And I declared this thing here and then let's say I set something up like
>> Kyle Simpson: So I make some reference to x inside of my function I've closed over x. X is gonna stay around for as long as that click handlers there and in our program we're never unassigning that thing. But let's say there's some other condition within our program. Which is that we would like to be able to garbage collect that array because we're done with it.
[00:02:48] We'd like that ten megabytes or one hundred megabytes or whatever of memory to be recollected. Normally you would do something like x equals null. That's no longer allowed when you use it const. So as a matter of fact there are places where the const can actually make it worse.
[00:03:04] You have to fiddle around with your garbage collection sorts of things in this particular case since it's an array you could have done something like x.that length equals zero. But if it were an object, you wouldn't have that you would have to four loop over them and delete all the properties or something like that.
[00:03:19] Super awkward. So there are places where const can actually trip you up, just like there are places where LET can to trip you up. So I'd recommend using the const in the places where it's actually justified. Rather than just preemptively using it everywhere and then getting tripped up.
>> Speaker 1: There's sort of a follow when using const with object trees.
>> Kyle Simpson: So I don't think it helps at all to add const and object dot frees. Okay, I don't think that does anything extra. If you want to, great, but I don't think it helps anything at all.
[00:03:49] I think because that comes at the beginning of the line, it's already signaled somebody to get confused as to what's going on.