🔰lexical environment

JSconceptexecution context ⟩ lexical environment

In JavaScript, every running function, code block {...}, and the script as a whole have an internal associated object known as the "lexical environment", it consists of two parts:

📗 JS.info ⟩ Lexical Environment

// make counter
function Counter() {

    let count = 0;

    return function() {
        return ++count;
    };
}

// a counter instance
let counter = Counter();

counter(),    // 1
counter(),    // 2

Last updated