Category: Javascript

Lexical Environment & Closure

Posted on December 20, 2021 by in Javascript

Code Blocks

If a variable is declared inside a code block {...} , it’s only visible inside that block.

For example:

We can use this to isolate a piece of code that does its own task, with variables that only belong to it:

For if, for, while and so on, variables declared in {…} are also only visible inside:

Nested Functions

A function is called “nested” when it is created inside another function. It is easily possible to do this with JavaScript. We can use it to organize our code, like this:

Here the nested function getFullName()  is made for convenience. It can access the outer variables and so can return the full name. Nested functions are quite common in JavaScript.

What’s much more interesting, a nested function can be returned: either as a property of a new object or as a result by itself. It can then be used somewhere else. No matter where, it still has access to the same outer variables.

Below, makeCounter  creates the “counter” function that returns the next number on each invocation:

Despite being simple, slightly modified variants of that code have practical uses, for instance, as a random number generator to generate random values for automated tests.
How does this work? If we create multiple counters, will they be independent? What’s going on with the variables here?
Understanding such things is great for the overall knowledge of JavaScript and beneficial for more complex scenarios. So let’s go a bit in-depth.

Lexical Environment

Every execution context has a reference to its outer environment, and that outer environment is called Lexical Environment.

We can say that whenever an execution context is created to execute the code along with it a lexical environment is also created by the JavaScript engine. Each lexical environment has reference to its parent lexical environments.

A lexical environment can be considered as a data structure which holds identifier-variable mapping (identifier => name of the variable/functions). It has two components:

  1. Environment Record – Actual place where the variable and function declarations are stored
  2. Reference To Outer Environment – it has access to its outer (parent) lexical environment. This component is the most important in order to understand how closures work.

A lexical environment object looks like this:


A closure is a function that remembers its outer variables and can access them. In some languages, that’s not possible, or a function should be written in a special way to make it happen. But as explained above, in JavaScript, all functions are naturally closures (there is only one exception, to be covered in The “new Function” syntax).

Usually, a function remembers where it was born in the special property [[Environment]]. It references the Lexical Environment from where it’s created (we covered that in the chapter Variable scope, closure).

But when a function is created using new keyword, its [[Environment]] is set to reference not the current Lexical Environment, but the global one.

When person()  is executed, the JavaScript engine creates a new execution context and a lexical environment for the function. After this function finishes, it returns displayName()  and assigns it to peter variable.

When the person()  finishes, its execution context is removed from the stack. But its lexical environment is still in the memory because its lexical environment is referenced by the lexical environment of displayName() . So name variable is still accessible.

Posted By: Shubham Kumar Jha, Osmosee

Displaying .rtf files in the browser

Posted on September 20, 2017 by in Javascript


The main aim of this blog is to show the content of an .rtf file on the browser window. Usually, when trying to open an .rtf file in the browser, it is downloaded instead of being displayed. This is because the browsers cannot reliably display RTF content. In order to overcome this problem, we came up with a solution which involves converting the contents of an .rtf file into HTML and then rendering it on the browser window.

Implementing routing using AngularUI Router

Posted on July 6, 2017 by in Javascript


In this blog, we’ll give you a rundown of how we’ve implemented routing in our single page project management tool – PineStem.

Routing is a process of navigating the user to the desired location based on the action they perform.
Maintaining the user’s navigation history is a major challenge while developing a single page application(SPA).
To overcome this problem, we implemented a simple stack based solution which only involves pushing and popping an object with required properties.