72

I have read, and discovered through my own experience, that JavaScript doesn't have a block scope. Assuming that the language was designed this way for a reason, can you explain to me what is that reason?

I've looked around on Google and here, but the posts I have found just reiterate that JS has a function scope and not block scope, without explaining why. I'm curious to know why this is actually the case.

2
  • 4
    The functional programming style in JavaScript doesn't encourage it I suppose plus JS was a rushed concept, but let is coming in ES6 so we will have block scope in the near future. Firefox already has support for let.
    – elclanrs
    Jun 26, 2013 at 4:48
  • The originator of the language answered the question and the explanation by @Aadit and myself seems to cover any other issues so I would consider it no longer opinion only.
    – mplungjan
    Jun 26, 2013 at 7:48

4 Answers 4

114
+50

Converting my comment to answer

Choice of the creator: I tweeted Brendan and got the following answer:

@mplungjan 10 days did not leave time for block scope. Also many "scripting languages" of that mid-90s era had few scopes & grew more later.


That said, here are some relevant points:

Important: JavaScript prior to ECMAScript2015 (6th edition) does not have block scope. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do, if you think they do anything like such blocks in C or Java.

we can artificially introduce scopes by creating new functions and immediately invoking them

let and const declared variables are hoisted but they are not initialized to undefined in the same way var is. Hence, referencing a let or const declared variable before its value is assigned raises a ReferenceError.

Redeclaration of the same variable in the same block scope raises a SyntaxError.

1
15

New answer as of 2015. ES6 does have block scope for variable definitions with the let and const keywords.

8
  • 2
    Unfortunately some users might still live in 2010, so relying on a brand new language feature is not the best practice.
    – riv
    Aug 5, 2015 at 13:25
  • 1
    @riv - Javascript is not only used in browsers where you have to code to the lowest common denominator. It can be used in environments like node.js where you control your entire execution environment and can use let now. Or, you can use one of the transpilers that lets you write code in ES6 and the transpiler converts it to ES5 compatible code so it will run in older browsers.
    – jfriend00
    Aug 5, 2015 at 14:37
  • Not a new answer, cause Js have block scope with try/catch from ES3
    – Trung
    May 4, 2016 at 8:57
  • @Trung - Can you provide a reference that explains that? I don't see any evidence of block scope for variable definitions.
    – jfriend00
    May 4, 2016 at 15:48
  • 1
    @Trung - That's a very special case (only in catch handlers that is not likely what the OP was asking about and cannot be used in any general purpose way to make block scoped variables of your own in regular blocks in ES3. If you think that answers the OP's question, then feel free to make your own answer.
    – jfriend00
    May 4, 2016 at 17:49
11

Block scope was not implemented for the following reasons:

  1. It's makes the language easier to implement. JavaScript was initially designed as a language for writing interactive web applications. Hence it needed to be small and easy to implement.
  2. Block scopes introduce a performance hit to dynamic languages like JavaScript. This is because when you try to access some variable which is not in the current scope JavaScript first checks the current scope, then the parent scope and so on until it either finds the variable or reaches the end. Hence the introduction of block scopes would make variable access in loops and nested loops very slow.
  3. The lack of block scopes makes it easier to write programs. For example say you want to create a variable only if a certain condition is true. All you need to do in JavaScript is declare and define the variable within an if statement. In languages like C you would have to declare the variable outside the if statement and define it within the if statement.
  4. The lack of block scopes allow declarations to be hoisted. This is especially useful in the case of function declarations. For example see this fiddle: http://jsfiddle.net/L6SgM/ (note however that this example doesn't work in Firefox).
  5. Since JavaScript supports first-class function expressions we don't need block scopes. They can be simulated using immediately invoked function expressions.
4
  • 3
    "Block scopes introduce a performance hit to dynamic languages like JavaScript." -- this is a property of an implementation, not a language. Maybe it held for the very first JavaScript implementation, but no engine written in the past ten years should suffer from it.
    – Marijn
    Jun 26, 2013 at 12:05
  • #5) strictly speaking, we don't "need" block scopes any more than we needed JavaScript in the first place ... thankfully the ECMA team seems to disagree by adding support for block scopes via the 'let' keyword ... simpler language constructs are always welcome. Jun 25, 2015 at 4:46
  • 3
    I : Could you give us the sources that prove Brendan Eich didn't implement block scopes in Javascript for the 5 reasons you mention? I suspect you are mixing historical reasons (#1) with your reasons (#2 to #5) . . . II : #3 is wrong: The variable is always created, only its initialization is conditional (see: jsfiddle.net/paercebal/yvwhu7r0 ). #4 is bogus (adding "use strict" ; in Chrome gives the error: Uncaught SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function.). #5 is a dirty workaround. . . .
    – paercebal
    Aug 6, 2015 at 21:54
  • It may make writing programs easier, but certainly not maintaining them.
    – mbomb007
    Mar 23, 2018 at 22:02
0

There are many reasons, but some that come to mind are to aid in parsing/debugging code that uses object literals (which can sometimes look like a block), and to simplify the garbage collection of local variables.

I hope that the promised support (discussed here, for example, http://esdiscuss.org/notes/2012-07-25) turns out to be real because it would be very convenient to use variables like i that were local to only a single loop.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.