r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

Show parent comments

-1

u/Aswole May 28 '19

One thing I've noticed when debugging from Chrome is that I can't access certain variables outside of a block unless I declared them with var (I'm guessing it has something to do with hoisting?). I still always use const/let, but there are times I will switch to var temporarily for that reason.

3

u/aust1nz May 28 '19

If you declare those variables using let or const first outside of the block, and then assign values to them within your block, they will be accessible in the outer scope without becoming globally assigned like with var.

4

u/DougWebbNJ May 28 '19

If "outside of the block" is the same as "at the top-level of the function's block", then let, const, and var are equivalent. That's the behavior I assumed Aswole was trying to achieve, by switching const/let to var without moving the variable definition.

1

u/Aswole May 29 '19

You correctly interpreted my poorly worded comment.