AppDividend
Latest Code Tutorials

JavaScript const vs let: The Complete Guide

0

ES6 brought two new ways to create variables in JavaScript,

  1. const
  2. let

Before ES6, there was only one way to declare and initialize the variable, which is through identifier var. The var has the following two scopes.

  1. global scope
  2. function scope

The scope defines where variables and functions are accessible inside of your program. In JavaScript let vs var post, we have discussed more on how var is different from let which is blocked scope.

JavaScript const is the identifier that can’t be reassigned. The let is an identifier that can be reassigned. If you don’t need to reassign, `const` is your by default option over `let` because you may want your code to be as straightforward as possible.

JavaScript const vs let

The `const` identifier is a signal that the variable won’t be reassigned. The `let` identifier is a signal that the it may be reassigned, such as a counter variable in a loop, or a value swap in an algorithm.

// app.js

let dana = 'Dana Scully'
console.log(dana)
dana = 'Fox mulder'
console.log(dana)

Output

Dana Scully
Fox mulder

In this example, you can see that first we declared and initialized the dana variable to Dana Scully and then reassigned to Fox Mulder. We logged both values, and we didn’t get any errors.

In the case of const, we can not reassign the variable.

// app.js

const dana = 'Dana Scully'
console.log(dana)
dana = 'Fox mulder'
console.log(dana)

Output

Dana Scully
/Users/krunal/Desktop/code/node-examples/es/app.js:3
dana = 'Fox mulder'
     ^

TypeError: Assignment to constant variable.
    at Object.<anonymous> (/Users/krunal/Desktop/code/node-examples/es/app.js:3:6)

We got the TypeError: Assignment to constant variable.

This means that we can not reassign the value to a const variable.

Don’t use `var` in the production code. Always use either const or let depending on your scenarios.

The `var` is now the weakest identifier when you define a variable in JavaScript. The `var` variable can be anything like

  1. It may or may not be reassigned.
  2. The variable may or may not be used for the entire function. Means can’t say whether it will change or not.
  3. The var is just for a block or loop.

So, it is better not to use var in JavaScript and instead use const and let.

JavaScript typeof

Javascript typeof is an inbuilt operator that returns the string, indicating a type of the unevaluated operand. With `let` and `const` in JavaScript, it’s no longer safe to check for an identifier’s existence using `typeof` operator.

// app.js

function foo () {
  typeof bar
  let bar = ‘baz’
}
foo()

Output

/Users/krunal/Desktop/code/node-examples/es/app.js:3
  let bar = ‘baz’;


SyntaxError: Invalid or unexpected token

Conclusion

You should always use `const` unless you know the variable is going to change in the future. The reason for using const is that you are indicating to yourself as well as any other developers that have to read your code that this variable shouldn’t change.

If your variable values keep changing, then you should use let.

That is it for Javascript const vs let comparison.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.