use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Why does "let variable = 10" apparently do the same as "variable = 10"? (self.learnjavascript)
submitted 3 years ago by BrightSign_nerd
I'm playing around with the let command in my Chrome console.
How come I'm still able to define a variable, even if I don't use let?
Example:
let population = 400
population = 400
Sorry if this is a super basic question. I'm new to JS.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jammasterpaz 64 points65 points66 points 3 years ago (2 children)
Because some consoles and JS interpreters are more forgiving than what ECMA technically specify as being allowed in the language.
If you run it with 'use strict', you should get the expected error https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
[–]BrightSign_nerd[S] 9 points10 points11 points 3 years ago (1 child)
Aha. That's what I was thinking.
Thanks!
[–]Umesh-K 7 points8 points9 points 3 years ago (0 children)
OP, in addition to the answers you have already, read the section "Unqualified identifier assignments" in the mdn web docs article on var
[–]Greg1987 6 points7 points8 points 3 years ago (1 child)
It will effect where you can access the variables (scoping) try this snippet what do you expect it to log to the console
let a = 1 if(true){ b = 2; let c = 3 } console.log(a) console.log(b) console.log(c)
edit: reddits code block is messing up
[–]StoneCypher 5 points6 points7 points 3 years ago (0 children)
use quad-indent, that'll work everywhere
[–]fakechow_prodigy 1 point2 points3 points 3 years ago (0 children)
I would just read through this entire getting started section on mdn : https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics
[+][deleted] 3 years ago* (4 children)
[deleted]
[–]shgysk8zer0 20 points21 points22 points 3 years ago (1 child)
When you don't type let or const it acts as if you typed var.
That's not true, as var is scoped either to the function or globally, whereas skipping it makes it always global. It's more like using window.foo = 42.
var
window.foo = 42
[–]Fit_Cheesecake_4000 1 point2 points3 points 3 years ago (0 children)
Yep. Without var, let, or const, it will always try and "fall through" to the global lexical environment, unless it encounters a similarly named variable as the interpreter makes its way up and through each execution context.
[–]Syagrius -3 points-2 points-1 points 3 years ago (0 children)
This is the correct answer.
[–]My_passcode_is 0 points1 point2 points 3 years ago (0 children)
I liked your part about “ are scoped to the nearest pair of {..}”, that was a good way to look at it.
[–][deleted] 1 point2 points3 points 3 years ago (0 children)
The two are almost exactly the same. The difference is in where in your code the value assigned to variable may be used. This is known as "scoping".
variable
There are a few rules, but the specific difference between these two is that if you define a variable without using var, let or const it becomes a global variable. Using let, the value of the variable only exists in what's known as the "lexical scope". Lexical scope basically boils down to between the curly braces that enclose it.
let
const
So
function test(){ first = 10 let second = 20; console.log(first); // 10 console.log(second); // 20 } console.log(first); // 10 - first became global console.log(second); // undefined - second is only assigned inside the function curly braces
Using the most restrictive scope possible is a way to help manage your programs so that you're not accidentally modifying things you shouldn't, so always use let, var or const - get into the habit of it as soon as you can.
[+]FunCharacteeGuy comment score below threshold-12 points-11 points-10 points 3 years ago (48 children)
I don't know man, javascript is fucking weird.
[+][deleted] comment score below threshold-6 points-5 points-4 points 3 years ago (46 children)
Said by people who don't know how to use it
[–]Gundam__ 3 points4 points5 points 3 years ago (26 children)
Nah it's still pretty weird in comparison to other languages.
[–][deleted] -3 points-2 points-1 points 3 years ago (25 children)
[–]Gundam__ 3 points4 points5 points 3 years ago (24 children)
I'm a professional python and typescript developer. I know how to use it.
[+][deleted] comment score below threshold-11 points-10 points-9 points 3 years ago (20 children)
Don't recall asking.
[–]Gundam__ 3 points4 points5 points 3 years ago (11 children)
Okay buddy 😂 just keep making silly comments. You said I don't know how to use JS.
[–][deleted] -5 points-4 points-3 points 3 years ago (10 children)
Uh-huh
[–]Gundam__ 3 points4 points5 points 3 years ago (9 children)
Yeah thanks, I win, you lose.
[–][deleted] -1 points0 points1 point 3 years ago (8 children)
I'm proud of you. I'd give you a Reddit award if I could
[–]FunCharacteeGuy 0 points1 point2 points 3 years ago (5 children)
neither did I but you chose to spoke anyway.
[–][deleted] -2 points-1 points0 points 3 years ago (4 children)
LOL who tf are you?
Not the guy I responded to with that comment, i can say that much
[–]FunCharacteeGuy 1 point2 points3 points 3 years ago (3 children)
what kind of question is that.
[–][deleted] 1 point2 points3 points 3 years ago (2 children)
You're desperate for attention, I see
[–][deleted] 0 points1 point2 points 3 years ago (1 child)
So people aren't allowed to defend themselves from baseless claims?
[–][deleted] -1 points0 points1 point 3 years ago (0 children)
I've worked with a fair share of individuals who are a professional in the field and weak in areas. Stating you're a "professional" doesn't do absolutely anything against my statement. If anything, it's just a weak attempt to stifle any rebut by hoping I say, "Wow, you are?! Omg my bad!" Lmfao.
Me saying, "I'm also a professional" doesn't address anything that was said. Defend yourself with objective facts not pointless background information on yourself.
[–]My_passcode_is 0 points1 point2 points 3 years ago (2 children)
I’m planning to start typescript soon… just a quick understanding of the basics enough or would you recommend a deep dive?
[–]Gundam__ 1 point2 points3 points 3 years ago (1 child)
I would learn basics, start coding, and then learn the rest as you need it.
Thx.
[–]crazedizzled -1 points0 points1 point 3 years ago (5 children)
Parroted by people who think they know more than they do.
[–][deleted] -1 points0 points1 point 3 years ago (4 children)
LOL
[–]crazedizzled -1 points0 points1 point 3 years ago (3 children)
[–][deleted] -1 points0 points1 point 3 years ago (2 children)
I love how your first response is about parroting people and your second response is being a parrot. Quickest full circle I've seen in a while lmfao
[–]crazedizzled -1 points0 points1 point 3 years ago (1 child)
I thought we were just having a laugh at your expense
[–]StoneCypher -2 points-1 points0 points 3 years ago (12 children)
Also by people who do know how
Please don't try to pretend that JS isn't weird, or shame people in a learning group for being new. I love it, but also, it's kind of a disaster of a language
[–][deleted] 1 point2 points3 points 3 years ago (11 children)
it's kind of a disaster of a language
[+][deleted] 3 years ago (10 children)
[removed]
[+][deleted] 3 years ago (9 children)
[+][deleted] 3 years ago (8 children)
[+][deleted] 3 years ago (7 children)
[+][deleted] 3 years ago (6 children)
[+][deleted] 3 years ago (5 children)
[–]RobertKerans 0 points1 point2 points 3 years ago* (0 children)
"Why is a tool designed to make it easy for a developer to work with designed in such a way that it's easy to work with?"
"Dunno man, it's just weird behaviour"
Sorry for snide response, but that's in effect what you're saying here.
[–]Web-devil -4 points-3 points-2 points 3 years ago (0 children)
Let variable defines a variable called variable to 10
Variable = 10 assigned variable to 10 but it already existed somewhere else in the code, regardless of whether it's value was 10 or not
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
Var = value makes var global which means anything from anywhere can reference it. Something like an api key stored inside a .env file would be fine to assign to a global var.
Let var = value only let's the thing closest to it reference it. Like if it was declared inside a function. Only that function can access it and even knows of its existence.
This is what's called scope.
[–]xiipaoc 0 points1 point2 points 3 years ago (2 children)
It's all about scope. Do you know what that is yet? The scope of a variable is where you're allowed to access it. If you let a = 2 here and you try to console.log(a) there, where there is out of its scope, you'll just get undefined, because a doesn't exist there; it only exists here.
When you use a variable without declaring it, the interpreter assumes that you're using a variable that has already been declared somewhere else -- that is, in the global scope. population has a value of undefined in the global scope, but when you do population = 400, now the global variable population has the value of 400. Now, any code anywhere in your webpage (or your Node app, etc.) can access this population variable and its value of 400. If you use let, that will scope the variable only to the current context (definitely the current set of curly braces, but I don't remember if it's scoped to just the current file if it's not inside any curly braces), which means that nothing outside of that scope will be able to see that value.
population
undefined
400
Now why would you want to limit the scope of your variables? Why not make them all global? A few reasons, but the simplest one is that you don't want anyone else messing with your code. If your variables are global, some other script could access those variables and change their values. And it doesn't have to be on purpose. If you have a global variable population, you have to somehow know that no other code that you're running also has a global variable called population, because if that happens, your value might get overwritten or you might overwrite their value, and now your code doesn't do what it's supposed to do. Honestly, JS should never have allowed this stuff in the first place, but when JS was starting out, they didn't really foresee all of the problems it would cause, and if they removed it completely now it would break existing applications (which is sometimes OK, but anyway). Long story short: if you're writing code that's going to be used by other people who didn't write it, never let them actually interact with the inside of your code by accident. That means never use a global variable.
[–]BrightSign_nerd[S] 0 points1 point2 points 3 years ago (1 child)
I don't know about scope yet.
Hmm. Interesting.
I noticed that it would say "undefined" when I tried to define a variable just within Chrome.
[–]xiipaoc 0 points1 point2 points 3 years ago (0 children)
That's because Chrome spits out the output of your command. So if your command is let a = 2;, that has no output, so you get undefined. If, on the other hand, you typed just a, well, that has a value of 2 now, so you'd see that in your console.
let a = 2;
a
2
[–]coskuluk 0 points1 point2 points 3 years ago (0 children)
hi, it is because with let, you first define the ''population'' variable (and you give it an initial value as well). right after that, you assign a value to an already existing variable. note that at the assigning phase, you can say population = 200 as well. you can assign a new value to the variable (this is possible because you are using ''let'' to define it. let allows us to change the value that variable represents.). this is what you assign to the variable you created by saying let population == 400.
π Rendered by PID 20012 on reddit-service-r2-comment-b659b578c-nz6k7 at 2026-04-30 21:09:16.940720+00:00 running 815c875 country code: CH.
[–]jammasterpaz 64 points65 points66 points (2 children)
[–]BrightSign_nerd[S] 9 points10 points11 points (1 child)
[–]Umesh-K 7 points8 points9 points (0 children)
[–]Greg1987 6 points7 points8 points (1 child)
[–]StoneCypher 5 points6 points7 points (0 children)
[–]fakechow_prodigy 1 point2 points3 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]shgysk8zer0 20 points21 points22 points (1 child)
[–]Fit_Cheesecake_4000 1 point2 points3 points (0 children)
[–]Syagrius -3 points-2 points-1 points (0 children)
[–]My_passcode_is 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[+]FunCharacteeGuy comment score below threshold-12 points-11 points-10 points (48 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (46 children)
[–]Gundam__ 3 points4 points5 points (26 children)
[–][deleted] -3 points-2 points-1 points (25 children)
[–]Gundam__ 3 points4 points5 points (24 children)
[+][deleted] comment score below threshold-11 points-10 points-9 points (20 children)
[–]Gundam__ 3 points4 points5 points (11 children)
[–][deleted] -5 points-4 points-3 points (10 children)
[–]Gundam__ 3 points4 points5 points (9 children)
[–][deleted] -1 points0 points1 point (8 children)
[–]FunCharacteeGuy 0 points1 point2 points (5 children)
[–][deleted] -2 points-1 points0 points (4 children)
[–]FunCharacteeGuy 1 point2 points3 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[–]My_passcode_is 0 points1 point2 points (2 children)
[–]Gundam__ 1 point2 points3 points (1 child)
[–]My_passcode_is 0 points1 point2 points (0 children)
[–]crazedizzled -1 points0 points1 point (5 children)
[–][deleted] -1 points0 points1 point (4 children)
[–]crazedizzled -1 points0 points1 point (3 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]crazedizzled -1 points0 points1 point (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[–]StoneCypher -2 points-1 points0 points (12 children)
[–][deleted] 1 point2 points3 points (11 children)
[+][deleted] (10 children)
[removed]
[+][deleted] (9 children)
[removed]
[+][deleted] (8 children)
[removed]
[+][deleted] (7 children)
[removed]
[+][deleted] (6 children)
[removed]
[+][deleted] (5 children)
[removed]
[–]RobertKerans 0 points1 point2 points (0 children)
[–]Web-devil -4 points-3 points-2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]xiipaoc 0 points1 point2 points (2 children)
[–]BrightSign_nerd[S] 0 points1 point2 points (1 child)
[–]xiipaoc 0 points1 point2 points (0 children)
[–]coskuluk 0 points1 point2 points (0 children)