0

In the below sample code,

let a: number = 1; // Global(G)
function f(){
    // let a: number = 1; // Enclosing(E)
    return function g(){
        // let a: number = 1;  // Local(L)
        return a;
    }
}

console.log(f()());

Does let keyword follow LEGB rule for variable lookup, similar to python?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • The identifier `a` is not resolved differently because it was declared with `let`. The only difference between `let` and `var` (wrt scope resolution) is that `let` allows to declare variables in block scopes. – Bergi Apr 24 '18 at 15:39
  • @Bergi First try using `const`, otherwise use `let`. Do not use `var`. This is the rule I follow. There is no chance to understand, how `var` works? All `var` keywords should be replaced with `let` in existing application – overexchange Apr 24 '18 at 15:40
  • NO - Javascript is not Python - please learn the scoping rules of the language(s) you are asking about. – Randy Casburn Apr 24 '18 at 15:40
  • @RandyCasburn But I see the above TypeScript code works according to LEGB rule. – overexchange Apr 24 '18 at 15:42
  • 1
    @overexchange No, it doesn't follow any complicated four-letter-rule. It just uses lexical scoping - there are no exceptions. The global scope just encloses all scope chains, and all builtins are defined in the global scope. – Bergi Apr 24 '18 at 15:48
  • @Bergi How different is LEGB from lexical scoping? It is same, because, every inner level can access its outer levels. – overexchange Apr 24 '18 at 15:54
  • @overexchange If it's not different, why would you need to give it a separate name? – Bergi Apr 24 '18 at 15:58
  • @Bergi Agreed. But I learnt that `let` uses lexical scoping after reading this [comment](https://stackoverflow.com/questions/50005678/scoping-rule-let-keyword-typescript-javascript?noredirect=1#comment87028291_50005678). Your referred question doesn't help answer this. – overexchange Apr 24 '18 at 16:08
  • @overexchange Everything uses lexical scope in JS. It doesn't matter what keyword the variable was declared with - `var`,`let`, `const`, `function`, … - only in what scope it is. – Bergi Apr 24 '18 at 16:20
  • @Bergi `var` does not follow lexical scoping. See the [example](https://www.typescriptlang.org/docs/handbook/variable-declarations.html) where `f` has parameter `shouldInitialize`. In this function `f`, `return x` is referring to inner level declaration(`var x = 10`). – overexchange Apr 24 '18 at 16:27
  • @overexchange `x` still is **looked up** using lexical scoping rules. Sure, `var` does declare the variable in the function scope, but that is not what you were asking about. – Bergi Apr 24 '18 at 16:30
  • @Bergi Lexical scoping rule, says, every inner level can access its outer level, unlike the example function(`f`) – overexchange Apr 24 '18 at 16:32
  • @overexchange Even in that example, the outer level does not access the inner level. `var` declares a variable in the function level despite being inside the block level, but still the variable is accessed as usual. – Bergi Apr 24 '18 at 16:34
  • @Bergi With `var` keyword, semantically yes, but syntactically no. This is why I use `let` keyword that syntactically/semantically show/follow lexical scoping. This makes a lot of difference, in debugging problem code. This is why, I do not prefer reading your referred question that talks about `var` keyword. – overexchange Apr 24 '18 at 16:35
  • @overexchange So is there anything left unclear? – Bergi Apr 24 '18 at 16:39
  • @Bergi In this syntax `function f(x) {}`. Is `x`, `var` declared or `let` declared? Yes, `x` is function scoped variable – overexchange Apr 24 '18 at 16:53
  • @overexchange Neither, it's declared as a function parameter? – Bergi Apr 24 '18 at 18:11

0 Answers0