The Lexical Scope of Javascript!

The Lexical Scope of Javascript!

Written on:

Javascript Scope Hard parts Lexical

Have you ever wondered why sometimes, this was undefined, while you thought it was there and that sometimes your functions work in weird ways. Well, then you’re at the right spot. We’ll be discussing Lexical Scopes, in this post.


What is Lexical Scope?

Javascript is basically a compiled language, The code you write is first compiled to AST then to a efficient binary, which is later executed by the JS Engine. All the variables and scopes are placed on their positions after declaration, So that they can be used by the engine while executing the program. All the declarations at the time of compilation are done in different scopes, For example:

var call = "Mr."

const Greet = function(name){
    const greeting = "Good Morning";
    console.log(`${greeting}, ${call} ${name}`)
}

Greet("Saurav")

So, What is lexical scope in here! Let’s break it down.

Execution takes place after this.

There are more steps, but let’s not discuss them now. I think you get what lexical scope is?

If still, You’ve got some doubts? Let’s discuss them, Tweet @sauravkhdoolia

Resources:

- Scopes

---

Please report any issues here