What is the lexical environment?
Lexical: "Relating to the words or vocabulary of a language" - Oxford English Dictionary
In programming, this means where a chunk of code physically sits will affect how it is interpreted and executed. In plain terms, a lexical environment is where the code appears and what surrounds it.
Each time you create a new function, this creates another lexical environment:
index.js
1
constouter=()=>{
2
//thereisalexicalenvironmenthere
3
constinner=()=>{
4
//thereisanewlexicalenvironmenthere
5
}
6
}
7
8
//codeonline2willbeinterpreteddifferentlytocodeonline4becausetheyareindifferentlexicalenvironments
9
This means there are can be potentially dozens or hundreds of lexical environments within one file.