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
🌐
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.
🌐
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 ...
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
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
Var let and const
I thought unlike described in the challenge ‘Explore Differences Between the var and let Keywords’ that let behaves in the way that you can override the variable. Instead const cannot be changed. In the challenge is said var can be overridden and let not. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
January 26, 2021
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
58
March 28, 2022
🌐
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 with legacy JavaScript code or need a function-scoped variable (though this is rare in modern JavaScript/TypeScript). Use let when you need a block-scoped variable that can be reassigned, especially within ...
🌐
freeCodeCamp
freecodecamp.org › news › var-let-and-const-whats-the-difference
Var, Let, and Const – What's the Difference?
April 2, 2020 - Just like var, let declarations are hoisted to the top. Unlike var which is initialized as undefined, the let keyword is not initialized. So if you try to use a let variable before declaration, you'll get a Reference Error.
🌐
npm
npmjs.com › package › xlsx
xlsx - npm
<body> <style>TABLE { border-collapse: collapse; } TD { border: 1px solid; }</style> <div id="tavolo"></div> <script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script> <script type="text/javascript"> (async() => { /* fetch and parse workbook -- see the fetch example for details */ const workbook = XLSX.read(await (await fetch("sheetjs.xlsx")).arrayBuffer()); let output = []; /* loop through the worksheet names in order */ workbook.SheetNames.forEach(name => { /* generate HTML from the corresponding worksheets */ const worksheet = workbook.Sheets[name]; const html = XLSX.utils.sheet_to_html(worksheet); /* add a header with the title name followed by the table */ output.push(`<H3>${name}</H3>${html}`); }); /* write to the DOM at the end */ tavolo.innerHTML = output.join("\n"); })(); </script> </body>
      » npm install xlsx
    
Published   Mar 24, 2022
Version   0.18.5
Author   sheetjs
🌐
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.
Find elsewhere
🌐
GitConnected
levelup.gitconnected.com › const-vs-let-vs-var-in-javascript-which-one-should-you-use-c56cf9b9e2a3
Const vs Let vs Var in Javascript. Which One Should You Use? | by Alex Richards | Level Up Coding
February 3, 2020 - In other words, if you try to change a const variable, you will get an error thrown back at you. If you set a const statement inside of a block, however, it will only remain inside of that block. Since const is block-scoped, this means that anything you try to do with a const variable can only be used within the block of code where you initially wrote it.
🌐
Board Infinity
boardinfinity.com › blog › a-quick-guide-to-var-let-and-const
What is Var, Let, Const in JavaScript | Board Infinity
July 9, 2023 - We utilize the let and const variables in the current javascript, which was included in the ES2015(ES6) update; it is now used more commonly in modern javascript than the var variable.
🌐
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 - 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.
🌐
OneCompiler
onecompiler.com › javascript
JavaScript Online Compiler & Interpreter
let mobiles = ["iPhone", "Samsung", "Pixel"] // accessing an array console.log(mobiles[0]) // changing an array element mobiles[3] = "Nokia" Arrow Functions helps developers to write code in concise way, it’s introduced in ES6. Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well. ... const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] const squaresOfEvenNumbers = numbers .filter((ele) => ele % 2 == 0) .map((ele) => ele ** 2) console.log(squaresOfEvenNumbers)
🌐
W3Schools
w3schools.com › js › js_let.asp
JavaScript Let
ES6 introduced the two new JavaScript keywords: let and const. These two keywords provided Block Scope in JavaScript: Variables declared inside a { } block cannot be accessed from outside the block:
🌐
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.
🌐
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 - Let’s go! ... Signal code intent (i.e. is this variable meant to change or not) Avoid unintended issues (e.g. hoisting, global object pollution) A variable should represent one thing. Declaring our variables with const helps us achieve this goal as we cannot change the assignment of our identifier once it has been initialized.
🌐
Web Dev Simplified
blog.webdevsimplified.com › 2020-01 › var-vs-let-vs-const
JavaScript Var vs Let vs Const
January 20, 2020 - While block vs function scoping ... of the major differences is that the variables defined by var are hoisted which means that they are available to be used even before they are defined....
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Var let and const - JavaScript - The freeCodeCamp Forum
January 26, 2021 - I thought unlike described in the challenge ‘Explore Differences Between the var and let Keywords’ that let behaves in the way that you can override the variable. Instead const cannot be changed. In the challenge is said var can be overridden and let not.
🌐
JavaScript in Plain English
javascript.plainenglish.io › the-battle-of-the-scopes-var-vs-let-vs-const-in-javascript-bf3b9411311e
The Battle of the Scopes: Var vs. Let vs. Const in JavaScript | by Ange IT | JavaScript in Plain English
March 26, 2024 - Let has block scope, is not hoisted, can be reassigned but not redeclared. Const has block scope, is not hoisted, cannot be reassigned or redeclared. Variables declared with the var keyword in JavaScript have function scope.