function f() { function g() { } }The adjective lexical is used synonymously with static, because both pertain to the lexicon (the words, the source) of the program.
function g() { } function f() { g(); }
var x = ...;establishes a binding for the variable x that maps the variable name to a value. In the following, the terms “variable” and “binding” are often used interchangeably. This is less precise, but makes explanations shorter. Bindings are characterized by:
Scope: The direct scope of a variable is the syntactic construct “in which” a binding has been created. What constructs form scopes depends on the programming language: any kind of block in most languages, only functions in JavaScript. One has two options when it comes to determining where else a variable is accessible, in addition to the direct scope:
function f() { var x; function g(y) { return x + y; } }
function g() { var x = myvar + 1; // (*) } function f() { var myvar = 123; g(); }
function f() { var x = "foo"; function g() { var x = "bar"; function h() { } } }
function f(x) { function g() { return x; } return g; } var myfunc = f("hello"); console.log(myfunc()); // output: hello