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
🌐
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
Discussions

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
constants - In JavaScript, why should I usually prefer 'const' to 'let'? - Stack Overflow
Why most of the time should I use const instead of let in JavaScript? As we know if we use const then we can't reassign value later. Then why not use let instead of const? More on stackoverflow.com
🌐 stackoverflow.com
JS <> const vs let = should I always use const?
It's not a matter of preference, rcommended, or ideal programming style. const and let have their own purpose. One is not absolute better than the other. It would depend on whether the contained (direct) value may be changed or not; or whether it needs to be protected from being changed or not - either by your own code, or by others' code which you don't have control of. If neither doesn't matter, there's no difference betwen using const or let. More on reddit.com
🌐 r/learnprogramming
14
8
November 6, 2022
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
🌐
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.
🌐
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.
🌐
W3Schools
w3schools.com › js › js_let.asp
JavaScript Let
ES6 introduced the two new JavaScript keywords: let and const.
🌐
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
82

Basically,

  • use let if the variable's value will change during the code
  • use const if it won't and you / your team want to use const in those situations in the project you're working on; it's a matter of style

If you do use const, then it's surprising how often it turns out that the guidelines above mean you use const because you end up not needing to change a variable's value (if you're following the usual rules of keeping your functions of reasonable size and such). (Well, it surprised me, anyway...)

Using const when the variable's¹ value is not meant to change accomplishes a few things:

  1. It tells others reading your code that you don't intend the value to change.

  2. It gives you a nice proactive error if you change the code so it writes to that variable. (A decent IDE can flag this up proactively, but if not, you'll get the error when running the code.) You can then make an informed, intentional decision: Should you change it to let, or did you not mean to change that variable's value in the first place?

  3. It gives a hint to the JavaScript engine's optimizer that you won't be changing that variable's value. While the engine can frequently work that out through code analysis, using const saves it the trouble. (Caveat: I have no idea whether this is actually useful to the JavaScript engine. It seems like it would be, but runtime code optimization is a very complicated and sometimes non-intuitive process.)


¹ Yes, it's funny to use the term "variable" to refer to something that by definition doesn't vary. :-) The specification's term is "binding," but I bet you won't hear people talking about "bindings" in everyday conversation anytime soon... So the aggregate term will probably remain "variable" except when we can specifically refer to something as a "constant."

2 of 6
50

Using const by default is essentially a matter of programming style. However there are pros and cons to each:

Arguments in favor of using const by default

The arguments in favor of using const by default are the following:

  1. It avoids side effects caused by involuntary reassignments;
  2. During a code review, it removes an uncertainty, because the developer who sees a const variable can count on the certainty that it will not be reassigned;
  3. Maybe we could say it is more consistant with functional programming and immutable states.
  4. With TypeScript, there are some cases with better inferences.

Here is an example of advantage when using const with TypeScript:

const hello = "Hello" as string | undefined
if (hello !== undefined) {
    ["A", "B"].forEach(
        name => console.log(`${hello.toUpperCase()}, ${name}`) // OK
    )
}

With let, in strict mode, TypeScript detects an error:

let hello = // …
// …
        name => console.log(`${hello.toUpperCase()}, ${name}`)
//                             ^__ error here: Object is possibly 'undefined'.

Arguments in favor of using let by default

I summarize here the article Use “let” by default, not “const” that gives arguments in favor of using let by default rather than const:

  1. Re-assignment is not a dangerous thing, it is just... usual;
  2. If a variable could be reassigned, then it should be declared with let, because it is more expressive to reserve const for real constants;
  3. const is misleading because it doesn’t stop references being modified;
  4. It is two more characters to write and to bundle;
  5. Using const by default is inconsistant with function parameters;
  6. There is no performance gain to use const.
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › let
let - JavaScript | MDN - Mozilla
Note that let is allowed as an identifier name when declared with var or function in non-strict mode, but you should avoid using let as an identifier name to prevent unexpected syntax ambiguities. Many style guides (including MDN's) recommend using const over let whenever a variable is not reassigned in its scope.
🌐
Fireship
fireship.dev › var-let-const
var vs let vs const in JavaScript
Finally the difference between let and const is that once you've assigned a value to const, you can't reassign it, but with let, you can. ... I know, another newsletter pitch - but hear me out.
🌐
CodeParrot
codeparrot.ai › blogs › javascript-var-vs-let-vs-const
JavaScript Var vs Let vs Const: Key Differences & Best Uses
September 1, 2024 - Use let only when a variable's value needs to change. In modern javascript var vs let vs const , developers favor const and let over var for more predictable, maintainable, and bug-free code.
🌐
Educative
educative.io › answers › difference-between-var-let-and-const-keyword-in-javascript
Difference between var, let, and const keyword in JavaScript
The const keyword is also block-scoped, hoisted but not initialized. Once assigned, const cannot be reassigned; however, if it holds an object or array, the properties can still be modified. The choice between var, let, and const depends on their scoping rules, hoisting behavior, and mutability. JavaScript ...
🌐
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....
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.

🌐
freeCodeCamp
freecodecamp.org › news › differences-between-var-let-const-javascript
var, let, and const in JavaScript – the Differences Between These Keywords Explained
October 28, 2020 - 😇 · If you're just starting out using JavaScript, a few things you may hear about these keywords are: var and let create variables that can be reassigned another value. const creates "constant" variables that cannot be reassigned another value.
Top answer
1 of 11
73

The difference between let and const is that once you bind a value/object to a variable using const, you can't reassign to that variable. In other words Example:

const something = {};
something = 10; // Error.

let somethingElse = {};
somethingElse = 1000; // This is fine.

The question details claim that this is a change from ES5 — this is actually a misunderstanding. Using const in a browser that only supports ECMAScript 5 will always throw an error. The const statement did not exist in ECMAScript 5. The behaviour in is either JS Bin being misleading as to what type of JavaScript is being run, or it’s a browser bug.

In practice, browsers didn't just go from 0% support for ECMAScript 2015 (ECMAScript 6) to 100% in one go — features are added bit-by-bit until the browser is fully compliant. What JS Bin calls ‘JavaScript’ just means whatever ECMAScript features your browser currently supports — it doesn’t mean ‘ES5’ or ‘ES6’ or anything else. Many browsers supported const and let before they fully supported ES6, but some (like Firefox) treated const like let for some time. It is likely that the question asker’s browser supported let and const but did not implement them correctly.

Secondly, tools like Babel and Traceur do not make ES6 ‘run’ in an older browser — they instead turn ES6 code into ES5 that does approximately the same thing. Traceur is likely turning const statements into var statements, but I doubt it is always enforcing that the semantics of a const statement are exactly replicated in ES5. Using JS Bin to run ES6 using Traceur is not going to give exactly the same results as running ES6 in a fully ES6 specification-compliant browser.


It is important to note that const does not make a value or object immutable.

const myArray = [];
myArray.push(1); // Works fine.
myArray[1] = 2; // Also works fine.
console.log(myArray); // [1, 2]
myArray = [1, 2, 3] // This will throw.

Probably the best way to make an object (shallowly) immutable at the moment is to use Object.freeze() on it. However, this only makes the object itself read-only; the values of the object’s properties can still be mutated.

2 of 11
26

What you're seeing is just an implementation mistake. According to the ES6 spec wiki on const, const is:

A initialize-once, read-only thereafter binding form is useful and has precedent in existing implementations, in the form of const declarations.

It's meant to be read-only, just like it currently is. The ES6 implementation of const in Traceur and Continuum are buggy (they probably just overlooked it)

Here's a Github issue regarding Traceur not implementing const

🌐
Medium
medium.com › javascript-scene › javascript-es6-var-let-or-const-ba58b8dcde75
JavaScript ES6+: var, let, or const? | by Eric Elliott | JavaScript Scene | Medium
November 21, 2024 - This is why I favor `const` over `let` in ES6. In JavaScript, `const` means that the identifier can’t be reassigned. (Not to be confused with immutable values.
🌐
Scaler
scaler.com › home › topics › javascript › difference between var, let, and const in javascript
Difference Between Var, Let, and Const in Javascript | Scaler Topics
March 9, 2020 - The main difference between Var, Let, and Const in JavaScript lies in their scope and mutability. Var is function-scoped and can be both re-declared and updated, making it the more flexible, yet potentially riskier option from older JavaScript ...