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
🌐
CodeParrot
codeparrot.ai › blogs › javascript-var-vs-let-vs-const
JavaScript Var vs Let vs Const: Key Differences & Best Uses
It is function-scoped, meaning a var variable is only accessible within the function where it is declared. If declared outside a function, it becomes global. Unlike let and const, which are block-scoped, var does not respect block boundaries (e.g., ...
🌐
Web Dev Simplified
blog.webdevsimplified.com › 2020-01 › var-vs-let-vs-const
JavaScript Var vs Let vs Const
We have talked a bunch about the ... to talk about. This difference is that variables defined by const can never be changed, while variables defined by let can be changed whenever....
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
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
[SOLVED] What is the difference between let, const and var? - Help & Support - PlayCanvas Discussion
I’ve starting so llong ago coding, that 3/4 of all languages I’ve learned practicaly doesen’t exist any more ( Pascal, Basic, 8bit assembler, cobol, VB?, Cobol a.s.o. ) Th las Languages I’ve “learned” are Go and typscript… Going back from TS To JS is getting quite hard… More on forum.playcanvas.com
🌐 forum.playcanvas.com
0
March 28, 2022
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
🌐
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.
🌐
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.
🌐
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 - Both let and const are block-scoped, meaning they are only accessible within the block they are declared in. var variables are function-scoped.
🌐
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.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_let.asp
W3Schools.com
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:
🌐
LinkedIn
linkedin.com › pulse › what-main-differences-between-let-const-var-keywords-js-pascal-allau
What are the main differences between the 'let', 'const' and 'var' keywords in JS ?
June 15, 2023 - In summary, `var` is used to declare variables with function or global scope, `let` is used to declare variables with block scope and reassignable value, while `const` is used to declare variables with block scope and constant (read-only) value.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-var-let-and-const-keywords-in-javascript
Difference between var, let and const keywords in JavaScript - GeeksforGeeks
This happens because let variables are hoisted but not initialized, so they remain in the TDZ until the declaration is executed. ... Hoisting with const: The variable x is declared with const, which is block-scoped and not hoisted.
Published   January 16, 2026
🌐
Medium
medium.com › @larry.sassainsworth › let-vs-var-vs-const-in-javascript-8b898f868394
Let vs Var vs Const in Javascript | by Larry Sass-Ainsworth | Medium
December 15, 2019 - Var declarations were the only type of variables before ES6, and Let and Const were introduced to fix some of its issues. The main differences with Var are its scope, its ability to be re-declared and updated, and the fact that it is hoisted.
🌐
GreatFrontEnd
greatfrontend.com › questions › quiz › what-are-the-differences-between-variables-created-using-let-var-or-const
What are the differences between JavaScript variables created using `let`, `var` or `const`? | Quiz Interview Questions with Solutions
September 5, 2021 - Variables declared using the var ... to the global object. let and const are block scoped, meaning they are only accessible within the nearest set of curly braces (function, if-else block, or for-loop)....
🌐
PlayCanvas
forum.playcanvas.com › help & support
[SOLVED] What is the difference between let, const and var? - Help & Support - PlayCanvas Discussion
March 28, 2022 - I’ve starting so llong ago coding, that 3/4 of all languages I’ve learned practicaly doesen’t exist any more ( Pascal, Basic, 8bit assembler, cobol, VB?, Cobol a.s.o. ) Th las Languages I’ve “learned” are Go and typscript… Going back from TS To JS is getting quite hard…
🌐
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 - ... In this example, x is declared ... block when we log it to the console. let which was added in ECMAScript 6 (ES6), offers a more consistent and block-scoped method of declaring variables....
🌐
SheCodes
shecodes.io › athena › 103395-difference-between-var-const-let-in-javascript
[JavaScript] - Difference between var, const, let in | SheCodes
Learn about the differences and usages of var, const, and let in JavaScript for variable declaration and assignment.
🌐
Danweaver
danweaver.dev › posts › var-vs-const-vs-let
The Differences Between var, const, and let
May 31, 2020 - The only difference between const and let is that let values can be reassigned and const values cannot. const foo = "bar" foo = "biz" //Uncaught TypeError: Assignment to constant variable.
🌐
Go
go.dev › doc › effective_go
Effective Go - The Go Programming Language
Variables can be initialized just like constants but the initializer can be a general expression computed at run time.
🌐
Fireship
fireship.dev › var-let-const
var vs let vs const in JavaScript
However, the only difference is that once you've assigned a value to a variable using const, you can't reassign it to a new value. ... The take away above is that variables declared with let can be re-assigned, but variables declared with const ...