JavaScript var, let, and const explained

preview_player
Показать описание

🔗 Links

⌚ Timestamps
00:00 - Introduction
00:47 - The basics of var
01:22 - The problem with var
03:55 - Comparing var to CSS selectors
04:41 - let
06:07 - const
07:04 - You can change a const, but not override it
08:01 - How to decide which one to use

#css

--

Come hang out with other dev's in my Discord Community

Keep up to date with everything I'm up to

Come hang out with me live every Monday on Twitch!

---

Help support my channel

---

---

I'm on some other places on the internet too!

If you'd like a behind the scenes and previews of what's coming up on my YouTube channel, make sure to follow me on Instagram and Twitter.

---

And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!
Рекомендации по теме
Комментарии
Автор

Another really fun one dude!

I do interviewing for my job and if I was to offer some unsolicited advice; a few more affirmation noises from you would really help ease the tension in these guest videos. Whenever you nod and smile, pairing it more frequently with a sound like "ahh", "uhuh", "got it!" goes a long way to keep you present in the dialogue and confirm to the guest subconsciously that this is still a conversation. They're a guest after all, so we're watching two people not one.

Additionally, looking directly into the lens is lovely when you're just addressing us, but you'll notice that none of your guests do that. They're always looking at you, but slightly off to the side. This is just standard learned body language for video calls, so if your guest is doing it, you should probably mirror it.

And I really love when you interject to clarify things it's really helpful.

Turabbo
Автор

Although they exist, there are very few situations where you would NOT want variable scoping - I changed var to let in all of my legacy code, and I think I can count on one hand the number of times it needed to remain var.

yogibarista
Автор

I, personally, prefer const over let and only use let if I need the variable to change within a single iteration of the function. I tend to set up const with a ternary if I need it conditional different for that single iteration but will always be that value for that iteration.

scottborrowman
Автор

Just wanna say how great of an explainer Chris Ferdinandi is. I purchased some of his courses and they were the only ones that really clicked for me in understanding JavaScript. Even sometimes when I hit a barrier of knowledge, Chris's site is my first go-to to check if he has a Blogpost or course about it.

ksaenable
Автор

I use const for constants and var for variables :D

By the way, I fully support JS content on this channel. (Pure JS, not that crap from frameworks).

CristianKirk
Автор

Thanks, Kevin! This has been an open tab since it came out. Finally watched it. Chris is so very good at these kinds of explanations. This one was very helpful.

bobmonsour
Автор

I agree with the comments and disagree with the host.
const unless need to be let.
and 'var' is just bad

No thumbs up. A first time for when I watch videos on this channel.

danj
Автор

After working with languages like C and C++, I started thinking of variables as pointers of memory storage, rather than value storage.

With that mindset, const is a constant reference to a memory pointer that can't be reassigned, but object properties/qualities stored in the pointer can be altered. This explains why arrays and objects (and scalar prototypes) can be changed, but scalar values cannot.

I also subscribe to const primarily, but I'm generally biased towards OOP, where most variables I work with are objects anyway, so I make very limited use of let. An example of such limited use is with traditional for and while loops, though for arrays of objects, using const (for const x of y {...}) is just fine.

stevenstraker
Автор

I always use 'const' by default. I've found over the years that if I feel compelled to use 'let' it's usually an indicator that my code might not be as robust as it could be. I'll never understand the "let" is two letters shorter argument. Any "savings" you gain from typing two fewer characters is swallowed up by host of other work that you need to do as a developer. No one is counting your keystrokes (at least, I hope not).

ripwolfe
Автор

As a developer who strongly prefers the functional paradigm, it's save to say that my opinions often times differ with Chris', but I always enjoy watching him and gaining insight on what other developers may lay value on. I also appreciate how he seperates facts from opinions and lays them out very clearly.

FurryDanOriginal
Автор

A small thing that also differs with these, is when a variable is declared in the global scope. If you are using var globally, that variable will attach to the window object, meaning you can access it by window.myVariable. let and const doesn't do this. That means, that should you be in the quite specific scenario of having to access the global variable by string, like this window["myVariable"], it can only be done with var. It is a quite narrow scenario, but I have come across the need of it myself.

eksperiment
Автор

I think const vs let should reflect the intent of the code, do you want it to change or not? Plus if one of my developers tried to commit code that relied on the fuzzy nature of var to work I would tell them to rewrite it so it didn't

lukerazor
Автор

I have never had any training so I found 'var' easy to remember and use it everywhere and rarely had any problems and when I do, I use const. But it's good to have a proper explanation!

itsPenguinBoy
Автор

As per your first example, without var, variables are puts into the global space. Clearly different than using var. var is function scoped.

Your example just happens to be in a global space.

justingiovanetti
Автор

My approach:
Is it a object?
Yes - const
No - Can it change at runtime?
Yes - let
No - const

The only place I use var for are closures which are like objects with private properties.

Also I don't 100% stick to this approach. I sometimes use var for the things declared in the few first lines globally and sometimes I change a variable into a object in those lines without making it a const, which I should not do.

kacperkonieczny
Автор

A big thing not mentioned is that var can be hoisted from anywhere in the code, which can cause issues, that’s why let is there . Const does not make sense in JavaScript, because it is not really a constant as it would be in other languages . I think of const as a good motivator / indicator to be mindful of where and when you declare a variable.

JosephCodette
Автор

To me, const is the default not because it is special, but because let is. I use let to let others know that the value is going to change down the line.

feldinho
Автор

Nice intro. Opinionated. But hey...aren't we all ;-)
Just a few things:

You _should_ but do not need to declare a variable. That is how 'global' jQuery ($) and Underscore (_) work. In general this is a good approach for global utility libraries.
Scoping has its uses but so do global variables or functions.
You can not re-assign a new value to a const. But if it is an object you still can add key-value pairs or modify the value of any key.
If you do not want that (say for lookup tables with constants) you can 'freeze' the const.

So: nice helpful intro...thanks for sharing. But IMHO it is slightly more complicated - apart from programming style ;-)

montebont
Автор

I use const when I'm defining html elements with querySelector.
What I think development comes down to is: programming is a language, and everyone has an accent. Your accent might be a refined English accent (think Patrick Stewart), while someone else might "sound" like Hank Hill. Personally, my accent would be similar to the cross-eyed guy from "The Waterboy."

CarlosHernandez
Автор

To be honest, I'm still experimenting with variables like const, var, and let, regardless of the number of courses I've taken and videos I've watched. It seems to me it's somewhat of a trend; I use const or var based on my current mood.

TomasMisura
welcome to shbcf.ru