Var, Let, Const - Understanding variable declaration in Javascript

Var, Let, Const - Understanding variable declaration in Javascript

I started learning programming a few months back. Started from python, and later got introduced to Javascript. Though the logic of your code in every programming logic remains same, there are some major syntactical differences.

For instance, in python you don't write variable keywords for variable declaration. Whereas in Javascript there are 3 different keywords for variable declaration: "let", "var", "const".

As a beginner I started with only "var", I used to do all my variable declaration with var. Later I've learnt a few key points to decide which of the keyword to choose for your variable declaration.

Var

Var declarations were the norm prior to the introduction of ES6. However, there are complications with variables specified with var. This prompted the creation of new methods for declaring variables. Before we get into those concerns, let's first have a better understanding of var.

Scope of Var

The term "scope" refers to the area in which these variables can be used. Globally scoped or function/locally scoped var declarations exist.

When a var variable is declared outside of a function, its scope is global. Any variable declared with var outside of a function block is available for use across the entire window.

When a variable is declared within a function, it is function scoped. This indicates it's available and can only be used within that function.

Var variables can be re-declared and updated within the same scope and won't get an error.

Problem with var

Since var variable can be re-declared, it sometimes causes bugs in your program if you update your variable by mistake; i.e., if you use the var variable again and update its value without knowing that it was declared before for some other value.

##Let For variable declaration, let is currently the preferred method. It's not surprising because it's a step forward for var declarations. It also solves the problems with var that we just discussed.

Scope

Let is blocked scope. A block is a section of code that is bounded by. Curly braces are the home of a block. Anything enclosed in curly brackets is considered a block. As a result, a variable declared with let can only be used within that block.

The value of let can be updated, but it cannot be re-declared. Similar to var, a variable declared using let, can be modified inside its scope. However, unlike var, a variable declared with let can't be re-declared within its scope.

Const

Variables declared with const variable maintain constant values. Moreover, const has some similarities with let.

Scope

Const declarations are block scoped. Similar to let declarations, const declarations can only be accessed within the block they were declared.

The value of a variable declared with const remains the same within its scope, it cannot be updated or re-declared.

Note: The behavior of const is somehow different when it comes to objects declared with const. While a const object cannot be updated, the properties of this objects can be updated.

Conclusion

As a quick tip, always declare your variable with const, and only change it to let if you want them to be updated later in the program. This helps in avoiding bugs caused by var declaratin.