const. It simply means that you have created a global variable.
In node environment, let’s run the following code snippet
a = "test";
The result shows
test, which means it works!
However, if we add
'use strict' to enter strict mode, such declaration method is prohibited
An error is thrown.
ReferenceError: a is not defined
This method is highly NOT recommanded because it is prohibited under strict mode and it will pollute your global environment. If your global environment contains too many useless temporary variables, your program will likely go into unpredicted errors and it will be a horrible experience to debug on such problem.
So, DO NOT use this way at all.
var to declare your variables.
var also has its limitations because the variables declared with
var is at function level.
What does it mean? See the following example.
Before running this piece of code, what do you expect the output of the function to be? Throwing an error? You might probably think that
count is declared within the for loop, it should not be accessible outside of the loop.
But sorry, if you use
var to declare a variable, the variable belongs to the function scope, which means that even though
count is declared within the for loop,
count still belongs to
test() function. Hence, as long as it’s within
count is accessbile!
Another evidence is that, no error is thrown at all even if you
console.log(count) before declaring it within a function! Since no error is thrown, it would be hard to trace when unexpected bug occurs.
The output shows
undefined instead of throwing errors! Instead, errors will be thrown if the
count is not declared at all!
let is introduced in ES6. It is scoped at block level, which resolves the difficulty you might encounter when using
let, the following code snippet correctly throws errors
ReferenceError: count is not defined
That is because
count variable become block-scoped.
count only exists in this for loop. It is a better way to use when declaring variables.
However, it also has its disadvantage.
var because incompatible
let will likely cause problems in old browsers and stop webpage rendering once error occurs.
const is also introduced in ES6. Same as
let, it is also scoped at block level. The only difference is that
const variable is a constant, whose values cannot be changed.
The above code snippet will throw error
TypeError: Assignment to constant variable.
In addition, if you are working with arrays or objects, it’s totally fine with constant declarations but modify it’s attributes or members later. Consider the following code snippet:
No error is occured. That is because the constant values of
b are their addresses in the memory, instead of their members or attributes. If we assign the address of
b as below, errors will then take place.
Therefore, as long as you do not point
b to another locations, no error would occur.
It is recommanded to do so to ensure that you are operating on the correct instance of object / array.
You should never ever declare a variable without
var should be avoided too unless you really have to consider browser compatibility issues.
The good practice is that you should always consider declaring a variable using
const by default helps you avoid unnecessary mistakes such as carelessly re-assign values to an important variable. unless you are sure that you would change its values later. Then the second option should always be