JavaScript Variables var, let, const In 90 Seconds #JavaScriptJanuary



Watch the entire #JavaScriptJanuary series from the beginning!

Var, Let, Const.. Why are there so many ways to declare a variable in JavaScript?

To understand that, we’ll need a short history lesson. Since the original version of JavaScript ES1 in 1997 we have had var to declare variables. And it was the only way to declare a variable until 2015, with the release of JavaScript ES6, when let and const were introduced. But that doesn’t mean that everyone started to use let and const in 2015. Most browsers at the time were not compatible with ES6. Now, in 2020 major browser compatibility for let and const is good. But be aware that there are still a couple of browsers that do not fully support these variables, namely Internet Explorer 10 and below, and Opera Mini.

So, how do they work? let and const are block scoped, which means that they cannot be accessed from outside of the block within which they are defined. A block is defined with curly braces.

Let’s start with const. const stands for constant. The value cannot be changed, but it is also not immutable. So I can declare const animal = ‘cat’ and now that cannot ever be changed. But if I declare const animals = [‘cat’, ‘dog’, ‘fish’], I can modify the content of the array, which is not changing the reference which is animals.

With let I can declare a variable and then change it later in the code. Again, let is block scoped, so in a for loop we can access the variable within the block but not from outside of the block.

var works the same as let except it is function or globally scoped. So with this same example, if I change let to var we can see that var is now globally scoped and is accessible outside of the for loop. But if we would wrap the for loop in a function and then call it, we will be able to access that variable within the function but not outside of the function.

With var and let, we can also declare a variable but not initially define it. But with const we cannot do that, it must be declared and defined at the same time.

Another thing to note about var is that when it is declared it gets added to the window object, but let and const do not.

A potential downside to using var is that it can be declared multiple times. When declaring a variable one would assume that it is the first time it is being declared. So if we forget that we have already declared a variable with the same name and declare it again we would overwrite the original variable. let and const fix this issue and do not allow you to declare the variable more than once within the same scope.
_____________________________________

📚 Learn to CODE in just a FEW months here:
Treehouse Discount Code: https://treehouse.7eer.net/codeSTACKr
_____________________________________

🛠️ Tools I use:
🟠 Theme: https://marketplace.visualstudio.com/items?itemName=codestackr.codestackr-theme
🟠 Font: STACKr Code (Exclusive to my VS Code Course – https://vsCodeHero.com)
🟠 SuperHero Extension Pack: https://marketplace.visualstudio.com/items?itemName=codeSTACKr.superhero-extensions

🤖 I now use AI-powered code completions thanks to Kite. You should too so grab it FREE: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=codestackr&utm_content=description-only
_____________________________________

💖 Show support!
PayPal: https://paypal.me/codeSTACKr
_____________________________________

Watch Next:
Web Development – Beginners Roadmap (2020) – https://youtu.be/iogabydg2y0
Playlist: Web Development For Beginners – https://www.youtube.com/watch?v=Ez4yHS2dsN8&list=PLkwxH9e_vrAJ0WbEsFA9W3I1W-g_BTsbt
_____________________________________

Connect With Me:
Website: http://www.codestackr.com
Twitter: https://twitter.com/codeSTACKr
Instagram: https://instagram.com/codeSTACKr/
Facebook: https://facebook.com/codeSTACKr
_____________________________________

** Affiliate Disclaimer: Some of the above links may be affiliate links, which may generate me a sales commission at no additional cost to you.

#JavaScriptJanuary #31Days31Videos #codeSTACKr

source