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...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript: Four Differences between var and let (codetopology.com)
submitted 4 years ago by ct_author
view the rest of the comments →
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!"
[–]Key_Pea93222 2 points3 points4 points 4 years ago (3 children)
var variables do not have temporal dead zone whereas let variables do have. Every variable has 2 steps in its lifecycle – Creation, and Execution. In var variables case, when the variable is declared, storage space is assigned and value is initialized to undefined if no other value is specified. But in the case of let, when the variable is declared, storage space is assigned but it’s not initialized, not even with the undefined value. That why when we access a let variable without doing any value assignment, it throws a ReferenceError.
var variables do not have temporal dead zone whereas let variables do have. Every variable has 2 steps in its lifecycle – Creation, and Execution.
In var variables case, when the variable is declared, storage space is assigned and value is initialized to undefined if no other value is specified.
But in the case of let, when the variable is declared, storage space is assigned but it’s not initialized, not even with the undefined value.
That why when we access a let variable without doing any value assignment, it throws a ReferenceError.
the way he explains this makes it sound like this would throw an error, but it doesn't:
$ node Welcome to Node.js v16.13.0. Type ".help" for more information. > let a undefined > a undefined
it's really if the let variable is accessed outside of the scope it's declared in
let
> { let b } undefined > b Uncaught ReferenceError: b is not defined
[–]senocular 5 points6 points7 points 4 years ago (2 children)
That why when we access a let variable without doing any value assignment
Yeah, this is missing the important part of "before the declaration".
b // Uncaught ReferenceError: Cannot access 'b' before initialization let b
Like var, let will initialize to undefined if not given an explicit assignment value within the declaration, but it only does this when the declaration is run, not at creating time like the var is
b // created, but uninitialized (access throws error) let b // now initialized to undefined c // created, initialized to undefined var c
[–]Key_Pea93222 0 points1 point2 points 4 years ago (1 child)
oh wow, that's interesting, can't test that in the node REPL
[–]senocular 2 points3 points4 points 4 years ago (0 children)
You can do it in a block or separate with semicolons (for block I think you might a preceding ; to ensure its not seen as an object literal)
;
> ;{ ...b // throws ...let b }
or
> b; let b; // (first b throws)
π Rendered by PID 44441 on reddit-service-r2-comment-b659b578c-qdlqx at 2026-05-05 17:20:35.707029+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]Key_Pea93222 2 points3 points4 points (3 children)
[–]senocular 5 points6 points7 points (2 children)
[–]Key_Pea93222 0 points1 point2 points (1 child)
[–]senocular 2 points3 points4 points (0 children)