Scope in JavaScript

Siva
TIL JS
Published in
6 min readJul 16, 2016

--

Today I am going to tell you about the scope and execution context in JavaScript. This topic is often overlooked by professional developers. And no tutorials for beginners will explain you about the scope and execution context. Before we begin, let’s see how a computer program works.

How a computer program works?

  1. High level Code: You write your program in a particular language (say javascript or java), then you save the file to run the program.
  2. Tokenizer: The compiler for your program reads the file and breaks them into small chunks of meaningful information. For example, let’s imagine the following code in JavaScript: var hello = ‘world’; The tokenizer will split the above line of code into var, hello, =, ‘world’ and ;
  3. Syntax Tree: The language compiler will then get the tokens from the tokenizer and creates an Abstract syntax Tree out of the tokens. For example with the above tokens, we can create a Syntax tree as follows:
Abstract Syntax tree model

4. Execution: The compiler uses the syntax tree and creates a machine code which can be understood by the machine. For example in the above code, the compiler might instruct the computer to reserve a memory for a variable named hello and store the value world in it.

You might be wondering, why I am taking a compiler theory lesson in a JavaScript post. We need…

--

--

Siva
TIL JS

JavaScript lover. Full stack developer. Failed Entrepereneur.