Home » Difference Between Var Let and Const

Difference Between Var Let and Const

Let Var Const Javascript

“var” is a new feature in ES6 that lets you declare a ‘variable’ and later change the value it contains. “let” is similar to var, but shares the same name as the function. “Const” is used to define immutable variables, which cannot be reassigned.

Var vs Let vs Const:

Var: declares a variable with an identifier;

Let: declares a local or temporary variable with an identifier and assigns it a value;

Const: creates an immutable variable with an identifier and stores its value in memory as part of its definition at compile time.

What is “var”?

Variable declaration in ES6 is a statement that declares a variable name and initializes its value. The var keyword is used to declare the variable.

var x = 3; // assigns 3 to the variable x
var y = 4; // assigns 4 to the variable y

Things we can do with “var” variable

var is one of the main building blocks of ES6. It helps developers with making their code more readable and understandable. With this new language, we can do many things such as:

  • Loop through an array of numbers and return the current index number
  • Use it as a storage space for objects
  • Change its value dynamically every time executed
  • Return multiple values from one function

How to declare functions with var variable?

When a function is declared with var, it means that it can be assigned a value later in the code.

When you are trying to create a function but cannot find the right syntax, you can use a var instead of defining a function.

Let’s say you want to return a value from a function, no matter what the value is. In order to do this, you must declare the function with a var variable.

In the following code snippet, we can determine if the variable is equal to 0 or 1 by using a conditional statement:

if (var == 0) print(“The variable is zero”)

else if(var == 1) print(“The variable is one”)

Hoisting of var variable?

Hoisting is a programming language feature that allows us to use variables before they are declared.

Hoisting can be really helpful in JS land because it saves developers time by not needing to define variables and then later use them.

However, there are some cases where hoisting may not be the best tool. This happens when hoisting prevents other node processes from running correctly, or when it creates side effects in your code. They can also interfere if the value of your variable changes during execution.

You have to be careful with hoisting when using ES6 and its new features like let and const which makes the variable more persistent on the stack.

x = 5; // Assign 5 to x
var x; // Declare x

Problem with var

“var” variables are mutable and can change their value at any time, even after they’ve already been declared.

The problem arises when you want to initialize a variable with an initial value that should be fixed and cannot change during program execution.

Variables as we know are nothing but identifiers that store values. With var, we can dynamically create variables. This is a pattern that’s known as the dynamic scope in javascript.

But there’s a problem with this abstraction of a variable that the programmers don’t fully understand. They are not aware of the implications of using it in certain situations and how it affects performance and execution time.

What is “let”?

“Let” is a new keyword in the ES6 specification that is reminiscent of an if statement. It is used like this:

let x = y (); // x = y
x ; // this will be undefined here
y ; // this will be undefined here

Although it is similar to an if statement, it has a few differences. “If” statements are evaluated after each execution and “let” statements are only evaluated once.

Let is a new reserved word in es6 that allows programmers to define and update variables.

Variables are typically defined as “let x = 3;” but this may change with the introduction of “let”.

This section is about how to let can be updated but not re-declared in es6. This means that, even if the first parameter changes, the second one is still the same.

Hoisting of let

Hoisting of let in es6 is a new feature that allows us to use let without the need for reassignment. It allows us to use let (in the global scope) without declaring it first.

While this feature is still not available in all browsers, it is available on all platforms, so developers can be confident using it and enjoy its benefits.

Hoisting of let in es6 will also save developers from having to move variables out of functions and back into the lexical scope as they would have had to do before.

What is “const”?

The “const” keyword is a new keyword in ES6. It can be used to define a constant value that cannot be changed once it has been assigned to the variable, and it is similar to “let” and “constant” keywords in other programming languages.

Constants are often used as a way of avoiding name clashes. For example, if you have the following:

var favorite number = 5;

var number to bypass = 999;

then the name “favorite number” could clash with the function’s name and would not work as expected.

However, by using const we can avoid this issue and keep our functions names tidy with no clashes.

Hoisting of “const”

The hoisting of “const” in es6 is an interesting topic. The question is whether or not it should be hoisted on global scope. Yes, it should be hoisted because it can be used as a function parameter without the need for a variable declaration.

It’s important to note that this code only works with const declared variables and not with variables defined using var or let.

The implementation of const in ES6 has been controversial, with some developers claiming it’s useless and will cause more code bugs than helpful, while others feel otherwise. The final decision on whether or not this feature should be implemented will most likely be left to the user base as developers go back and forth about which approach is best.

Related Blogs