Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGreg Schudel
4,090 Pointswhat is let in javascript?
I'm seeing the words "let" and "const" used alot in the current course I'm on (Javascript and the DOM). I'm sure this is novice of me, but what exactly is "let" and "constr"? What are the differences and usages within Javascript?
I've looked on MDN around the internet and feel like I have some inclination but not completely clear. here's a quote from one individual:
const
is a signal that the identifier wonβt be reassigned.
let
, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block itβs defined in, which is not always the entire containing function.
var
is now the weakest signal available when you define a variable in JavaScript. The variable may or may not be reassigned, and the variable may or may not be used for an entire function, or just for the purpose of a block or loop.
Is the safe to assume that it is correct? What is subjective and needless to know? Sorry for the confusion, I just want to be objective as possible without running into gray areas of people's opinions (there is alot of that on the internet about "code standards" now adays).
2 Answers
Jacob Mishkin
23,118 Pointswhat you have is completely correct. Think if it as do I want this variable to remain constant then use const. does this variable need to change in the course of the variable then use let. Var in Js is kinda being driven out, but you can still use it if you want to to. What const and let are doing is helping Js devs scope better when programming. Use var until you feel comfortable using const and let.
Aidan Reed
2,956 PointsWhat I don't quite get with let and var is the scope. I totally understand the block level scoping of let, say for example in a loop but then after that I'm like meh. If you define a variable using let outside of a function it can be used anywhere much like var. If you define a variable using let inside a function or block it remains in that block. But if you define a variable using var inside a function it still can only be used in that block so other than uses with loops is that the only difference?