Understanding the Differences between let, var, and const in JavaScript

Ashwini Paraye
3 min readJun 6, 2023
let | var | const

In JavaScript, there are three keywords used for declaring variables: let, var, and const. While they may seem similar at first, they have some important differences in terms of scoping, reassignment, and behavior. In this article, we will explore these differences.

Scoping:

  • let: Variables declared with let are block-scoped, meaning they are only accessible within the block of code where they are defined. Blocks are typically enclosed within curly braces {}.
  • var: Variables declared with var are function-scoped, making them accessible throughout the entire function where they are defined, even outside of any block.
  • const: Like let, const also has block scope, restricting its accessibility to the block of code in which it is defined.

Reassignment:

  • let: Variables declared with let can be reassigned to different values. This means you can change the value of a let variable later in your code.
  • const: Variables declared with const have a constant value that cannot be reassigned after being defined. Once you assign a value to a const variable, it remains unchanged throughout the program.
  • var: Similar to let, var variables can also be reassigned like let variables.

Hoisting:

  • let and const: Variables declared with let and const are not hoisted, which means you must declare them before using them in your code. If you try to access them before their declaration, you will encounter an error.
  • var: Variables declared with var are hoisted to the top of their scope. This means you can access them before their declaration, although they will have an initial value of undefined.

Redeclaration:

  • let and const: Within the same block or scope, redeclaring a variable with the same name using let or const will result in an error. Each block has its own scope, and variables declared within it should have unique names.
  • var: Unlike let and const, var allows you to redeclare a variable within the same scope without any issues. This behavior can sometimes lead to unintended consequences and bugs.

Global Scope:

  • let, const, and var: When variables are declared outside of any function or block, they have global scope. However, there is a subtle difference.
  • var: Variables declared with var in the global scope are attached to the global object (e.g., window object in a browser environment). This means they can be accessed from anywhere in your code.
  • let and const: Variables declared with let or const in the global scope are also accessible globally, but they are not attached to the global object. This can be advantageous in avoiding naming conflicts and keeping the global namespace clean.

Conclusion:

Understanding the differences between let, var, and const is essential for writing clean and maintainable JavaScript code. Remember that let and const provide block scope, with let allowing reassignment and const prohibiting it. var provides function scope and allows reassignment. Consider the specific requirements of your code to choose the appropriate variable declaration that best suits your needs.

I hope you found this article helpful. Thank you for reading! 😀

Happy Coding !

--

--

Ashwini Paraye

Fullstack Developer | 👨‍💻 Writing about programming concepts | React.js | JavaScript | Vue.js | Golang