Mon Jan 21 2019

LET vs CONST vs VAR in JavaScript

Programming4429 views

LET vs CONST vs VAR in JavaScript

To be a better coder, it's important to keep things simple. The way variable declarations work has been that weird part of programming in JavaScript. Variables creation depend on how you declare them, and ES6 offers options to make controlling scope easier. The 6th version of ECMAScript launched in June 2015 and it was the biggest transition of JavaScript.

In this post, we explain the important the three keywords LET, CONST, and VAR in JavaScript and will find the differences to use them perfectly.

So, let's get started -

Const

Const behaves in a very similar way to let. It is also block scoped and cannot be used until declared. There is, however, one key difference. Once variable declared using const keyword is assigned a value, you cannot reassign it. You also need to initialize the variable immediately when declaring it.  It is useful as this prevents accidental reassignment of the variable. It also promotes good coding practices as it prohibits using a single variable for multiple purposes during its lifecycle, which is confusing and error-prone. While this is a useful concept, you need to be aware of some limitations. The only restriction const provides is about reassignment. That does not mean an object assigned to a const variable is not immutable! You can still change its properties, delete them or add new ones. You just cannot assign a completely different object.

Var

The JavaScript variables statement is used to declare a variable and, optionally, we can initialize the value of that variable. Variable declarations are processed before the execution of the code. The scope of a JavaScript variable declared with 'var' is its current execution context. The scope of a JavaScript variable declared outside the function is global. The var status can be accessed anywhere in the function. 'var' has function level scoping and can change the value reference. If 'var' is defined outside the function it has a global scope so it can be accessed at anywhere so inside the for loop var num is accessible so a value of num is changed and when we console.log(num) outside the for loop it is value is changed.

Let

The let statement declares a local variable in block scope. It is similar to var, in that we can optionally initialize the variable. 'let' is preferred for variable declaration now. 'let' can be updated but not re-declared. A variable declared with let can be updated within its scope. ‘Let’ is often the best option for declaring variable were the value is likely to change similar to how var works. The difference being ‘Let’ variables are block-scoped so helps defend against those var scoped errors. The let statement allows you to create a variable with the scope limited to the block on which it is used. It is similar to the variable we declare in other languages like Java, .NET, etc.


 

You can share your experiences with us in the comment section. Thank you!

Photograph by Teguh Jati Prasetyo

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.