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 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-var-let-and-const-keywords-in-javascript
Difference between var, let and const keywords in JavaScript - GeeksforGeeks
Hoisting with let: The code logs x before it is declared with let, causing a Reference Error. This happens because let variables are hoisted but not initialized, so they remain in the TDZ until the declaration is executed.
Published   November 11, 2021
🌐
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.
🌐
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 ...
🌐
CodeParrot
codeparrot.ai › blogs › javascript-var-vs-let-vs-const
JavaScript Var vs Let vs Const: Key Differences & Best Uses
Use const by Default: Always default to const for variables that don’t need to be reassigned. 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.
🌐
Educative
educative.io › answers › difference-between-var-let-and-const-keyword-in-javascript
Difference between var, let, and const keyword in JavaScript
Lines 4–8: The let variable y ... block-scoped, similar to let, but with one major difference: variables declared with const cannot be reassigned after initialization....
🌐
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
🌐
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 - Here if we will declare a var variable or let variable, then it can be updated, but if we declare a const variable, it will not be updated in any case and will work fine with our function.
🌐
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 - In this regard, const is different from var and let. const is used for declaring constant variables – which are variables with values that cannot be changed. So such variables cannot be redeclared, and neither can they be reassigned to other values.
🌐
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:
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › let
let - JavaScript | MDN
However, this combination of var and let declarations below is a SyntaxError because var not being block-scoped, leading to them being in the same scope. This results in an implicit re-declaration of the variable. ... The left-hand side of each = can also be a binding pattern. This allows creating multiple variables at once. ... const result = /(a+)(b+)(c+)/.exec("aaabcc"); let [, a, b, c] = result; console.log(a, b, c); // "aaa" "b" "cc"
🌐
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 - No Hoisting: Similar to let, const declarations are hoisted, but accessing them before their declaration in the code results in a ReferenceError. No Reassignment: Unlike var and let, once a variable is assigned with const, it cannot be reassigned.
🌐
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 - In contrast to var, both let and const are block scoped. Any code bound by curly braces is a block. This means when we declare a variable with let or const it's scope is whatever pair of curly braces it was declared in.
🌐
CodeWithHarry
codewithharry.com › tutorial › var-let-const-in-js
var vs let vs const | JavaScript Tutorial
In this example, the y variable is declared with the let keyword and is only accessible within the block of the if statement. If you try to access it outside of the block, you will get a ReferenceError because y is not defined in that scope. The const keyword was also introduced in ES6 and ...
🌐
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 - As a result, if we declare a variable with const, we cannot: ... Therefore, each and every const declaration must be initialized at the moment of declaration. ... Const declarations are raised to the top, similar to let, but they are not initialized.
🌐
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.