It's actually a completely valid question. Most other languages only give a block scope to local variables.
In fact this behaviour is so confusing that later versions included a new, preferred declaration statement (marked with let instead of var), which works like in real programming languages and would indeed give a lookup error in this examples.
let/const actually enforce block scope, whereas var hoists to the top of the function. If you take a look at transpiled code meant for compatibility, it becomes clearer (I'm doing this by hand for readability):
I could have it slightly wrong, but back when they introduced the new keywords, this is how I remember it being explained to me. Tbh it was this that got me to switch over from CoffeeScript.
EDIT[1]: I had it wrong, like I thought. The code is better edited to reflect it.
EDIT[0]: fixed the function name, and also an article from last year that explains it:
Var is weird, but python has something similar which is actually pretty useful. You can declare variables in things like with blocks and they will work outside of them, meaning you don't have to declare a variable, then with open(filename, 'r') as file: and read to variable. You can simply read to variable and it will work outside of the with clause
Interesting. Is this something python folks like? I work with other languages, and I don't think I would want this. Sounds potentially ambiguous to the next guy reading the code. Need to think on it more tho, that is just my gut reaction
I would compare this to Java's try with resources. Both of them close the resource when you are no longer in the scope of the block. It's handy, so you don't forget to actually close them.
Yes, with statements as a whole are fine and comparable to try with resource, as you say. I am talking specifically about the ability to declare a var inside a with block, yet access it from outside that block. That's the piece I don't want. It creates ambiguous readability for no good reason that I've heard yet. I'd rather see things scoped intuitively, that's all.
I am happy they stopped asking why there are random global variables when you have html elements with ids or why the value of `this` in a non-bound function is sometimes undefined, sometimes window. Or better yet, why you had to check for undefined like 'typeof x === 'undefined'`
This is not hoisting. The op is wondering why is there not a syntax error on L1c8. It is actually a feature for labeling blocks. It is not a variable declaration.
Ain't it just that var is global scope? No hoisting weirdness here, though everything else is pretty weird (why break funcName? Isn't just break enough?)
var defines the variable with a larger scope than the block that it's in, so the variable still exists after the block is broken out of. If the variable was defined with let or const, it would cease to exist after the break, which would then cause the console.log to throw an exception.
Like the old joke says, coding in JavaScript is like trying to cross a street, look to both sides for safety and when you start crossing you get hit by a flying submarine
If changed to let a = 0; Js would most likely write "undefined" as variables can be created dynamically. console.log would create a, but it would have no value defined therefore "undefined"
343
u/[deleted] Jan 01 '22
I’m pretty sure op is wondering why it works and not why it doesn’t work taken from the context of their comment at the bottom of the post.
159
u/starbur-n Jan 01 '22
Called hoisting. Oddity of var in JS.
155
u/g_hi3 Jan 01 '22
that's exactly why they introduced the
letkeyword, right? because variables can be declared in a lower scope withvar36
u/Accidentallygolden Jan 01 '22
Can someone explain for a non js specialist?
8
u/Voltra_Neo Jan 01 '22
Oh god label + goto + hoisting + auto attach to windo
6
u/IamBananaRod Jan 01 '22
Like the old joke says, coding in JavaScript is like trying to cross a street, look to both sides for safety and when you start crossing you get hit by a flying submarine
15
u/Sir_Redrick Jan 01 '22
You can label code blocks using this syntax, It may come useful when you need to exit a couple of loops at the same time, useful link
21
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 01 '22
Image Transcription: Online Code Question
[Transcriber's note: The code in the following question is not in a monospace font, however, I have placed it in one for ease of readability.]
Javascript question
Why is the result here 0? Shouldnt it be a syntax error?
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
5
u/[deleted] Jan 01 '22
As a JavaScript developer, this makes me want to murder someone
(Legal disclaimer: This is a joke)
4
u/shanster925 Jan 01 '22
Javascript: Semicolons matter, until they don't.
2
u/x-sol Jan 01 '22
If changed to let a = 0; Js would most likely write "undefined" as variables can be created dynamically. console.log would create a, but it would have no value defined therefore "undefined"
1
u/Rai1redit Jan 01 '22
Finally a post where i actualy understand whats wrong with the code
0
u/[deleted] Jan 01 '22
[deleted] — view deleted comment
0
u/[deleted] Jan 01 '22
[deleted] — view deleted comment