Thus it lets us easily change the type of variables or date we have.
// 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:
- Compiler Overhead Optimization
- Side Effect Propagation
In modern JS engine we have two phases:
- Baseline Compilation (= Interpreter)
- 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.
The lesson here is that as much as we can, we should avoid changing the type of data we use.
Since we have:
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 youtube.com
Update: Fri Sep 06 2019 09:51:30 GMT+0430 (Iran Daylight Time)