JavaScript Definitions II


  1. Garbage Collection in JavaScript
  2. Single-Threaded
  3. Lexical Environment
  4. Scope chain
  5. Leakage of global variables

1. Garbage Collection in JavaScript

JavaScript is a Garbage Collected Language.

Within a function, we create an object and an object gets stored in our memory heap automatically. If we finish calling the function, it will be clean up automatically. only the data that is still useful to us remains. However, there is no perfect system, so it gives JavaScript developers the false impression that they do not care about memory management.

function subtractTwo(num){
 var human = "me";
 return num -2;

When the function is done and it is popped off from the stack then we don’t need human variable anymore. So, the Garbage collector is going to mark and sweep (clean up) the variable from our memory heap.

Mark and Sweep Algorithms

2. Single-Threaded

JavaScript is a single threaded programming language. Being single-threaded means that only one set of instructions is executed at a time.

What problems do you see with single-threaded synchronous code?

with single-threaded synchronous code that JavaScript engine runs it is going to make it really difficult if we have long-running tasks. Thus, we need JavaScript Run Time.

In the background, Web API is running while the synchronous JavaScript code is running.

Web APIs are what we call Asynchronous you can instruct the API to do in the background and return data once it is done.

setTimeout(() => {console.log("2")};
JavaScript Engine with Event Loop

3. Lexical Environment

Lexical environments contain an environment record in which the variables are stored and a reference to their parent environment.

Lexical environments build a tree structure.

function b(){
  function a(){
   return 0;
return 0;
  • In this case function a is lexically inside the b function.

In JavaScript, without this keyword, lexical scope is important where data and variable is defined.

  • Dynamic scope: where the function is called.

4. Scope chain

Each execution context has a link to its outside world or a link to its parent depends on where the function sits.

// Global variable
var x = "x";

function findName(){
 var b = "b";
 return printName();
function printName() {
 var c = "c";
 return "Me";
function sayMyName() {
 var a = "a";
 return findName();

All of these functions are going to be able to access the variable the global variable x.

function findName can not access variable c and a.

function printName can not access variable b and a.

function sayName can not access variable b and c.

All the functions have their own variable environment. They have access to each their own variables but they also have little link what we call a scope chain. The scope chain links and gives us access to variables that are in our parent environment.

In JavaScript our lexical scope where function was defined determines our available variables.

  • Not where the function is called (dynamic scope)

The idea of lexical scope which is also called static scope in a language means that only by looking at the source code we can determine which environment the variables in data are available. That is what the compiler does the JavaScript compiler looks at the code and attaches all these scope chain. Even before runs the code.

Scope Chain

Variables declared outside a function in what we call global scope and they can accessed in any other scope that inside of functions.

5. Leakage of global variables

The height is not been declared in function and global scope but the variable will declare for us. Thus, this is something that in past with JavaScript. It causes a lot of problems.

function weird(){
 height = 50;
 return height;

This is actually called leakage of global variables.

Prevent the problem

The "use strict" directive was new in ECMAScript version 5.

use strict was introduced as a way to prevent JavaScript for doing some unpredictable cases.

Leave a Reply

Your email address will not be published.