JavaScript the Fun Part



We know that JavaScript is a dynamic typed language.

Thus it lets us easily change the type of variables or date we have.

For example

// declare a variable using let
let name = "JS"; // type is string

// change the type to number
name = 2019

If we change the type of variable often, we encounter two problems:

  1. Compiler Overhead Optimization
  2. Side Effect Propagation

In modern JS engine we have two phases:

  1. Baseline Compilation (= Interpreter)
  2. Compiler Optimization (= Compiler)

In fact at first JS engine reads the JS code as an interpreter and for optimizing it one more time tries to compile it to machine code.

So the second phase is for making the code faster.

If we change the type of data we have, then the optimization phase cannot be succeeded and passes the code to another phase which is de-optimize phase.

Result? Overhead

The lesson here is that as much as we can, we should avoid changing the type of data we use.

Since we have:

  1. var
  2. let
  3. const

the best one for us will be const keyword.

I know that const keyword for Array and Object has no effect and cannot prevent them from modification but as a best practice it is preferable to use const.

And not just for avoiding compiler overhead but also to practice Functional Programming technique that we become familiar with it in other posts.

For better understanding of what I am saying please watch this video on

Franziska Hinkelmann: JavaScript engines - how do they even? | JSConf EU

Update: Fri Sep 06 2019 09:51:30 GMT+0430 (Iran Daylight Time)