News for hackers. Stuff that matters.

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

I'm currently reading "Data structures and Algorithms with Javascript" and I had a WTF! moment when I got to page 9 and read "...Javascript does not have a block scope..."

Meaning this piece of code works! Blimey!

  for(var i=0; i<=10; i++){
    // do nothing
  }

  // access 'i' out of for loop
  console.log("i = " + i); // i = 11
( 16 comments | 22 points | on HN )
Re:Ask HN: Am I one of the few JavaScript programmers who di... by michaelmartin on Sunday April 06, 2014 @07:17AM (#7540598)
ES6 introduces the 'let' keyword which gives you block scope: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

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

Re:Ask HN: Am I one of the few JavaScript programmers who di... by wilsonfiifi on Saturday April 05, 2014 @05:34PM (#7538540)
This post from stackoverflow explains the language design choice a bit further: http://stackoverflow.com/questions/17311693/why-does-javascr...
Re:Ask HN: Am I one of the few JavaScript programmers who di... by timothya on Saturday April 05, 2014 @05:17PM (#7538494)
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.
Re:Ask HN: Am I one of the few JavaScript programmers who di... by josso on Saturday April 05, 2014 @07:24PM (#7538872)
Yep, this and hoisting is something I expect any JavaScript developer to know.
Re:Ask HN: Am I one of the few JavaScript programmers who di... by sAuronas on Tuesday April 08, 2014 @05:01PM (#7555988)
And semicolon insertion.
Re:Ask HN: Am I one of the few JavaScript programmers who di... by sgentle on Saturday April 05, 2014 @09:24PM (#7539230)
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.
Re:Ask HN: Am I one of the few JavaScript programmers who di... by yulaow on Saturday April 05, 2014 @10:59PM (#7539514)
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?
Re:Ask HN: Am I one of the few JavaScript programmers who di... by nickporter on Saturday April 05, 2014 @07:28PM (#7538884)
Yeah, but there's this:

  array.forEach(function(value, index) { ... });
Re:Ask HN: Am I one of the few JavaScript programmers who di... by slashnull on Sunday April 06, 2014 @04:06AM (#7540228)
Yeah, that's a function call.
Re:Ask HN: Am I one of the few JavaScript programmers who di... by eximius on Saturday April 05, 2014 @08:04PM (#7538990)
Hopefully, but probably not. There are probably only slightly less bad JavaScript programmers as there are bad PHP programmers.
Re:Ask HN: Am I one of the few JavaScript programmers who di... by dsschnau on Thursday April 10, 2014 @04:37PM (#7569537)
Hey, cool. I guess I knew that but didn't really 'know' it. Thanks.
Re:Ask HN: Am I one of the few JavaScript programmers who di... by workhere-io on Sunday April 06, 2014 @01:56AM (#7539965)
Same thing with Python and PHP.
Re:Ask HN: Am I one of the few JavaScript programmers who di... by lugg on Monday April 07, 2014 @03:30AM (#7545090)
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!"

Re:Ask HN: Am I one of the few JavaScript programmers who di... by workhere-io on Monday April 07, 2014 @08:41AM (#7546110)
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.

Re:Ask HN: Am I one of the few JavaScript programmers who di... by lugg on Monday April 07, 2014 @06:43PM (#7550141)
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.

Re:Ask HN: Am I one of the few JavaScript programmers who di... by karishmasibal on Thursday April 10, 2014 @02:42PM (#7568741)
I hate Javascript... and recently I have gone mad over Node.js "server side javascripting..."