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

When should you use "var", "let", or "const" in JavaScript code - Stack Overflow
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 just use one throughout? Made some searches on google and apparently var is for old browsers but I want to find out from actual developers, which one is best to use in this day and age? Or it doesn't really matter? ... Check this. Explained with example ... More on stackoverflow.com
🌐 stackoverflow.com
What is the difference between 'var', 'const' and 'let'?
Hi! I am a beginner in JavaScript. Can anyone explain what is the difference between ‘var’, ‘const’ and ‘let’ when initializing variables? A simple idea is just enough :blush: More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
1
October 28, 2020
Understanding var/let and const
“let” and “const” are relatively new to JS, and are what you should use now. If the materials you’re learning from use “var” and don’t explain very soon why not to, find some newer materials. “var” works in subtly different ways that can be confusing and prone to errors, mainly dealing with variable scope. See this post for some more in-the-weeds explanation of var vs let. More on reddit.com
🌐 r/Frontend
20
9
April 10, 2022
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
🌐
freeCodeCamp
freecodecamp.org › news › var-let-and-const-whats-the-difference
Var, Let, and Const – What's the Difference?
April 2, 2020 - While this is not a problem if you knowingly want greeter to be redefined, it becomes a problem when you do not realize that a variable greeter has already been defined before. If you have used greeter in other parts of your code, you might be surprised at the output you might get. This will likely cause a lot of bugs in your code. This is why let and const are necessary.
🌐
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.
🌐
CodeParrot
codeparrot.ai › blogs › javascript-var-vs-let-vs-const
JavaScript Var vs Let vs Const: Key Differences & Best Uses
September 1, 2024 - It ensures clarity in your code, prevents accidental changes, and signals to others that the value remains constant throughout its lifecycle. Use let for Reassignment: Opt for let when you know a variable’s value will change, such as in loops or conditions. It combines flexibility with block-scoping, reducing the risk of variables leaking out of their intended scope. Avoid var: Avoid using var in modern JavaScript due to its function-scoping, hoisting, and redeclaration issues, which can lead to unpredictable behavior.
🌐
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 ...
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_let.asp
JavaScript Let
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Before ES6 (2015), JavaScript did not have Block Scope. JavaScript had Global Scope and Function Scope. 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:
🌐
Scaler
scaler.com › home › topics › javascript › difference between var, let, and const in javascript
Difference Between Var, Let, and Const in Javascript | Scaler Topics
June 21, 2024 - In the example above, we have an if block with a true condition, and inside the if block, we declare a variable name myName. Now when we try to print the myName variable to the console, it prints successfully, but when we try to print the variable outside the if block, we get a Reference Error. The main difference between Var, Let, and Const in JavaScript lies in their scope and mutability.
🌐
Educative
educative.io › answers › difference-between-var-let-and-const-keyword-in-javascript
Difference between var, let, and const keyword in JavaScript
While const prevents reassignment, it doesn’t make the variable’s content immutable if it’s an object or array: ... Let’s discuss when to use which keyword. Use var when working with legacy code or if compatibility with older JavaScript environments is required.
🌐
freeCodeCamp
freecodecamp.org › news › differences-between-var-let-const-javascript
var, let, and const in JavaScript – the Differences Between These Keywords Explained
January 11, 2023 - Here, number is a globally scoped variable declared with const. By trying to access this variable before the line of declaration, we get ReferenceError: Cannot access 'number' before initialization. The same will occur if it was a locally scoped variable. Here's an article to learn more about Hoisting in JavaScript with let and const – and How it Differs from var.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › let
let - JavaScript - MDN Web Docs
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.
🌐
Appsmith
community.appsmith.com › content › guide › variables-javascript-comprehensive-guide-var-let-and-const
Variables in Javascript: A Comprehensive Guide to Var, Let, and Const | Appsmith Community Portal
August 11, 2023 - Let's explore these three methods of variable declaration in JavaScript: var, let, and const, diving into when and why to use each, along with their respective advantages and drawbacks.
🌐
Medium
medium.com › @jackpritomsoren › difference-between-var-let-and-const-in-javascript-c6236899ca4d
Difference between var, let and const in JavaScript | by Jack Pritom Soren | Medium
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 › @adarsh-d › differences-between-let-const-and-var-in-javascript-5ab152904784
Let vs Const vs Var in JavaScript: Understanding the Differences | by Adarsh Dayanand | Medium
March 17, 2023 - function example() { ... declared (i.e., within curly braces). let variables can be reassigned, but const variables cannot be reassigned after they have been initialized....
🌐
Quora
quora.com › When-should-I-use-const-and-let-instead-of-var-in-Javascript
When should I use const and let instead of var in Javascript? - Quora
Answer (1 of 10): var Don't use it. The only thing var gives you is weird unexpected behaviors. For example [code]for (var i = 0; i
🌐
DEV Community
dev.to › chintanonweb › mastering-javascript-variables-a-deep-dive-into-let-var-and-const-866
Mastering JavaScript Variables: A Deep Dive into let, var, and const - DEV Community
April 23, 2024 - It's generally recommended to use let and const instead for clearer code semantics and better scoping. A: No, variables declared with const cannot be reassigned after initialization. However, for objects and arrays, you can modify their properties or elements without reassigning the variable itself. A: Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during compilation, regardless of where the actual declaration occurs within the code.