

Ask HN: Am I one of the few JavaScript programmers who didn't realize this - wilsonfiifi

I&#x27;m currently reading &quot;Data structures and Algorithms with Javascript&quot; and I had a WTF! moment when I got to page 9 and read &quot;...Javascript does not have a block scope...&quot;<p>Meaning this piece of code works! Blimey!<p><pre><code>  for(var i=0; i&lt;=10; i++){
    &#x2F;&#x2F; do nothing
  }

  &#x2F;&#x2F; access &#x27;i&#x27; out of for loop
  console.log(&quot;i = &quot; + i); &#x2F;&#x2F; i = 11</code></pre>
======
michaelmartin
ES6 introduces the 'let' keyword which gives you block scope:
[https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Refe...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Statements/let)

Browser compatibility isn't too bad, but no Safari support it seems:
[http://kangax.github.io/es5-compat-
table/es6/#let](http://kangax.github.io/es5-compat-table/es6/#let)

------
wilsonfiifi
This post from stackoverflow explains the language design choice a bit
further: [http://stackoverflow.com/questions/17311693/why-does-
javascr...](http://stackoverflow.com/questions/17311693/why-does-javascript-
not-have-block-scope)

------
timothya
Yes, it should be one of the first things you learn when learning JavaScript.
JavaScript only has function scope, meaning that you only get new scope inside
of a function.

~~~
josso
Yep, this and hoisting is something I expect any JavaScript developer to know.

~~~
sAuronas
And semicolon insertion.

------
sgentle
Blimey is right. Although I would guess for most people their first encounter
would be something like this:

    
    
      var foo = function(callback) { callback(); }
      
      for(var i=0; i<=10; i++){
        setTimeout(function() {
          console.log("i = " + i);
        }, 100);
      }
    

Which, if you don't know what to look for, is a bit of a brain melter. Between
that and [1,2,10].sort(), there are lots of great reasons to yell at your
screen when learning JS.

------
yulaow
Correct me but, atm, it would be possible to use the keyword "let" to declare
a local variable with a block scope if I remember well, isn't it?

------
nickporter
Yeah, but there's this:

    
    
      array.forEach(function(value, index) { ... });

~~~
slashnull
Yeah, that's a function call.

------
eximius
Hopefully, but probably not. There are probably only slightly less bad
JavaScript programmers as there are bad PHP programmers.

------
dsschnau
Hey, cool. I guess I knew that but didn't really 'know' it. Thanks.

------
workhere-io
Same thing with Python and PHP.

~~~
lugg
Am I the only one who doesn't see a huge problem with this? Its actually kinda
handy sometimes to have the last value / index of your array defined outside
after the loop. Same goes for variables defined within the loop.

Aside from that: Function scope makes sense to me but I dont think I've ever
gone and thought "damn it all, if only I had block scope!"

~~~
workhere-io
_Am I the only one who doesn 't see a huge problem with this?_

I don't think anyone really sees a problem with it, although it might be
somewhat of a gotcha for people coming from languages where variables are
local to the loop.

~~~
lugg
And it doing the opposite would be a gotcha for anyone else. Is anyone able to
explain why a language would have block scoping in the first place? Is it a
good thing and I just don't know? I really hope its not because of the
braces.. That would be kind of silly.

As it is, without that explanation, all I can think is, so weird language x
does something weird and then its a gotcha when a language (funnily enough
javascript) does it in a non weird way? I'd say sounds more like a gotcha for
language x, than anything else.

------
karishmasibal
I hate Javascript... and recently I have gone mad over Node.js "server side
javascripting..."

