In my opinion nowadays it is better to use let and const.

According to the ES6 standard, these options are more convenient with their scope. Both give us the so-called block scope. This means that the scope of their use can be limited to a block of code, such as a for loop or an if expression. This gives the developer more flexibility in choosing the scopes of variables.

In addition, if we consider these options, then you will not be able to change the variable declared with const, unlike var and let.

Generally speaking, let and const are often used now, depending on the need.

Answer from nixanosize on Stack Overflow
🌐
Medium
medium.com › swlh › the-difference-of-var-vs-let-vs-const-in-javascript-abe37e214d66
The Difference of “var” vs “let” vs “const” in Javascript | by Megan Lo | The Startup | Medium
October 26, 2020 - let welcome = 'Welcome to Sweater Season' const snoopy = 'Snoopy looks so cute with sweaters!!'welcome = 'Pumpkin Spice Latte! Halloween! Red Leaves!'snoopy = 'Snoopy looks ugly in sweaters!' // TypeError: Assignment to constant variable. ... Ooof, Javascript gone mad when you tryna reassign const variable, so as Snoopy.
🌐
Overreacted
overreacted.io › on-let-vs-const
On let vs const — overreacted
December 22, 2019 - A rule like “always use const where it works” lets you stop thinking about it and can be enforced by a linter. Reassignments May Cause Bugs: In a longer function, it can be easy to miss when a variable is reassigned. This may cause bugs. Particularly in closures, const gives you confidence you’ll always “see” the same value. Learning About Mutation: Folks new to JavaScript often get confused thinking const implies immutability.
Discussions

Difference between var, let and const
Using the function keyword creates a function declaration. Function declarations are hoisted to the top of the scope, thus the following code works fine: add(1, 2) function add (a, b) { return a + b } There are readability advantages to using the function keyword as it clearly communicates that this is a function. We can also create an anonymous function expression and, because functions in JavaScript are first class citizens, assign a function to a named variable with let, const or var. Keep in mind though, variables declared with let and const are hoisted, but not initialized with a value. Whereas variables declared with var are hoisted and initialized with the value undefined. So because add is invoked prior to it's initialization in the code below we encounter an error. With let/const: add(1, 2) // Reference error - cannot access 'add' before initialization const add = function (a, b) { return a + b } With var: add(1, 2) // Type error - 'add' is not a function (as it is initialized as undefined when hoisted) var add = function (a, b) { return a + b } If you're assigning a function to a variable I can't think of any situation where you wouldn't use const. Functions should always be pure and immutable whenever possible. Given a certain input, the output should always be consistently the same. Using const protects you from accidentally reassigning a variable containing a function expression. Compare this to use of the function keyword, which allows you to declare a named function twice, overiding the previous: function add (a, b) { return a + b } function add (a, b) { return a - b } add(2, 1) // 1 This is a disadvantage of function declarations versus expressions. Provided you understand the pros/cons and how hoisting works, it is really personal choice whether you prefer function declarations versus expressions. More on reddit.com
🌐 r/learnjavascript
31
57
March 28, 2022
When should you use "var", "let", or "const" in JavaScript code - Stack Overflow
New to JavaScript and a beginner. I came across JavaScript variables and realized there are three different ways to go about them (var, let and const). Do they each have a specific purpose or you can More on stackoverflow.com
🌐 stackoverflow.com
javascript - How much should I be using 'let' vs 'const' in ES6? - Software Engineering Stack Exchange
I've been writing a lot of ES6 code for io.js recently. There isn't much code in the wild to learn from, so I feel like I'm defining my own conventions as I go. My question is about when to use co... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
April 9, 2015
JavaScript variables: var and let and const
I know it can be confused with `var`, but no one should ever use that now that `let` exists · One of these days I'll write my own little language that compiles to TypeScript or something and fix that :) More on news.ycombinator.com
🌐 news.ycombinator.com
67
61
March 9, 2020
🌐
Reddit
reddit.com › r/learnjavascript › difference between var, let and const
r/learnjavascript on Reddit: Difference between var, let and const
March 28, 2022 -

Hi everyone!

I'm pretty new to web development and have started to read a little bit about javascript. Right now I'm trying to learn how to declare a function but realized that there is different ways to do this using var, let or const. Since I'm new to this, most of the explanation on the internet is pretty advanced to understand atm, so could anyone explain the differences for me very short short and easy, alternatively give me a link that explain it in pretty easy terms?

Thanks in advance!

(Edit: Thank you so much for the answers, rly appreciate it!)

Top answer
1 of 5
52
Using the function keyword creates a function declaration. Function declarations are hoisted to the top of the scope, thus the following code works fine: add(1, 2) function add (a, b) { return a + b } There are readability advantages to using the function keyword as it clearly communicates that this is a function. We can also create an anonymous function expression and, because functions in JavaScript are first class citizens, assign a function to a named variable with let, const or var. Keep in mind though, variables declared with let and const are hoisted, but not initialized with a value. Whereas variables declared with var are hoisted and initialized with the value undefined. So because add is invoked prior to it's initialization in the code below we encounter an error. With let/const: add(1, 2) // Reference error - cannot access 'add' before initialization const add = function (a, b) { return a + b } With var: add(1, 2) // Type error - 'add' is not a function (as it is initialized as undefined when hoisted) var add = function (a, b) { return a + b } If you're assigning a function to a variable I can't think of any situation where you wouldn't use const. Functions should always be pure and immutable whenever possible. Given a certain input, the output should always be consistently the same. Using const protects you from accidentally reassigning a variable containing a function expression. Compare this to use of the function keyword, which allows you to declare a named function twice, overiding the previous: function add (a, b) { return a + b } function add (a, b) { return a - b } add(2, 1) // 1 This is a disadvantage of function declarations versus expressions. Provided you understand the pros/cons and how hoisting works, it is really personal choice whether you prefer function declarations versus expressions.
2 of 5
22
All you need to remember is that var should never be used in modern JavaScript. Functions should either use the function keyword or const (with arrow functions). Objects/arrays should always be const. Just always use const until you can’t.
🌐
SheCodes
shecodes.io › athena › 124459-why-are-we-using-let-and-not-const
[JavaScript] - Why are we using let and not const? - | SheCodes
In programming, let and const are both used to declare variables. Find out why we choose let over const and when to use each one.
Find elsewhere
Top answer
1 of 6
185

My reply here is not javascript-specific.

As a rule of thumb in any language that lets me do so in a semi-easy way I'd say always use const/final/readonly/whatever it is called in your language whenever possible. The reason is simple, it's much easier to reason about code when it is dead obvious what can change and what cannot change. And in addition to this, in many languages you can get tool support that tells you that you are doing something wrong when you accidentially assign to a variable that you've declared as const.

Going back and changing a const to a let is dead simple. And going const by default makes you think twice before doing so. And this is in many cases a good thing.

How many bugs have you seen that involved variables changing unexpectedly? I'd guess a lot. I know that the majority of bugs that I see involve unexpected state changes. You won't get rid of all of these bugs by liberally using const, but you will get rid of a lot of them!

Also, many functional languages have immutable variables where all variables are const by default. Look at Erlang for example, or F#. Coding without assignment works perfectly in these languages and is one of the many reasons why people love functional programming. There is a lot to learn from these languages about managing state in order to become a better programmer.

And it all starts with being extremely liberal with const! ;) It's just two more characters to write compared to let, so go ahead and const all the things!

2 of 6
57

Be careful, because const object keys are mutable.

From here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

object keys are not protected

consider this example:

const colors = {red: "#f00"}; 
console.log(colors); // { "red": "#f00" }

colors.red = "#00f";
colors.green = "#0f0";
console.log(colors); // { "red": "#00f", "green": "#0f0" }

Same thing for arrays:

const numbers = [1, 2, 3];
console.log(numbers); // [ 1, 2, 3 ]

numbers.push(4);
console.log(numbers); // [ 1, 2, 3, 4 ]

I haven't decided totally myself, but I'm considering using const for all non-array/non-objects and use let for objects/arrays.

🌐
Medium
dev-aditya.medium.com › which-is-better-const-vs-let-in-javascript-500eb3991f68
Which is better const vs let in Javascript | by Aditya Yadav | Medium
October 2, 2024 - In JavaScript, both const and let are used to declare variables with block scope introduced in ES6 (ECMAScript 2015). While they share many similarities, they have key differences that affect how you use them in your code. However, when it comes to memory efficiency, there is no significant difference between const and let.
🌐
DEV Community
dev.to › kjdowns › var-let-and-const-what-s-the-difference-31om
var, let, and const - What's The Difference? - DEV Community
February 17, 2021 - Since variables are hoisted to the top of their scope, variables declared with var, let, and const are hoisted differently. Variables declared with var are hoisted to the top of their scope, and given an initial value of undefined. This means that if you try and use the variable before it is defined in your code, you will be using a variable with a value of undefined. Unlike var, JavaScript does not initialize a value for variables declared with let.
🌐
freeCodeCamp
freecodecamp.org › news › var-let-and-const-whats-the-difference
Var, Let, and Const – What's the Difference?
April 2, 2020 - Like let declarations, const declarations can only be accessed within the block they were declared. This means that the value of a variable declared with const remains the same within its scope.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-var-let-and-const-keywords-in-javascript
Difference between var, let and const keywords in JavaScript - GeeksforGeeks
When using let in a loop, each iteration of the loop creates a new instance of the variable. This is different from var, which shares the same variable across all iterations. ... Trying to access i outside the loop causes an error since it’s not available outside the block where it was declared. Arrays with const in JavaScript: The const declaration makes the reference to the numbers array constant, but its contents can still be modified.
Published   January 16, 2026
🌐
DEV Community
dev.to › jps27cse › difference-between-var-let-and-const-in-javascript-53c
Difference between var, let and const in JavaScript - DEV Community
September 20, 2023 - Use let and const instead of var when writing JavaScript code to prevent unexpected scope problems and create more predictable, maintainable code. If you need to reassign a variable, use let; if you wish to declare constants, use const.
🌐
Medium
medium.com › @robinviktorsson › understanding-the-differences-between-var-let-and-const-in-javascript-and-typescript-0ddd90c0b672
Understanding the Differences Between var, let, and const in JavaScript and TypeScript 💻 | by Robin Viktorsson | Medium
March 10, 2025 - ... Use var only if you are working ... rare in modern JavaScript/TypeScript). Use let when you need a block-scoped variable that can be reassigned, especially within loops or conditions....
🌐
Ibexoft
ibexoft.com › blog › var-vs-const-vs-let-in-javascript
Var Vs Const Vs Let In JavaScript — Ibexoft
April 3, 2023 - Unlike var, variables declared with let are block-scoped, meaning that they can only be accessed within the block they are declared in. This makes let a safer and more reliable option than var, as it eliminates the possibility of variables being accidentally overwritten or redeclared. Additionally, let variables can be updated without being redeclared, making them more flexible than const variables. Const, short for “constant,” is another ES6 addition to JavaScript.
🌐
C# Corner
c-sharpcorner.com › article › difference-between-let-var-and-const-in-javascript-with-example
Difference Between let, var, and const in JavaScript with Example
March 5, 2024 - Var keywords can be declared as globally scoped or function scoped, while let and cost are block scoped. Var variables can be re-declared or reassigned, while let variables can only be re-assigned but cannot be re-declared with the same name in the same scope. Const cannot be re-declared or re-assigned.
🌐
Hacker News
news.ycombinator.com › item
JavaScript variables: var and let and const | Hacker News
March 9, 2020 - I know it can be confused with `var`, but no one should ever use that now that `let` exists · One of these days I'll write my own little language that compiles to TypeScript or something and fix that :)
🌐
Hello Ajahne
ajahne.github.io › blog › javascript › 2018 › 03 › 22 › const-let-var-best-practices.html
CONST, LET, and VAR Best Practices - Hello Ajahne
March 22, 2018 - We also leave a message (i.e. signal) in our code to our future selves and future developers that this variable should not be reassigned. const is not a value that doesn’t change, but rather it is an identifier that does not get reassigned.
🌐
CodeLawd's Blog
codelawd.hashnode.dev › explained-the-difference-between-var-let-and-const-in-javascript
Explained - The Difference Between Var, Let and Const in JavaScript
November 24, 2025 - // Cannot be redefined const name = "Jane Doe"; name = "Sam Doe"; console.log(name) // Uncaught TypeError: Assignment to constant variable. While programming in JavaScript it is a good practice not to define variables as global variables. This is because it is possible to inadvertently modify the global variable from anywhere within the JavaScript code. To prevent this one needs to ensure that the scope of the variables are limited to the code block within which they need to be executed. In the past before keyword let was introduced as part of ES6, to circumvent the issue of variable scoping using var, programmers used the IIFE pattern to prevent the pollution of the global name space.
🌐
JavaScript in Plain English
javascript.plainenglish.io › var-vs-let-vs-const-in-javascript-which-one-should-you-use-1bdf6d67cd37
var vs let vs const in JavaScript — Which One Should You Use? | by Akiko Kawai | JavaScript in Plain English
December 2, 2025 - And yet… this is one of the most misunderstood and most misused topics in JavaScript 😵‍💫. So today, let’s break it down cleanly, practically, and with examples you can remember forever. ... Before ES6 (2015), we only had var. And that caused tons of bugs. function test() { var x = 10; console.log(x); } console.log(x); // ❌ Error ... Introduced in ES6, let fixed most of var’s issues. if (true) { let x = 10; } console.log(x); // ❌ Error ... When you don’t want a value to change, use const.