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
🌐
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:
🌐
W3Schools
w3schools.com › js › js_const.asp
JavaScript const
Declaring a variable with const is similar to let when it comes to Block Scope. The x declared in the block, in this example, is not the same as the x declared outside the block: const x = 10; // Here x is 10 { const x = 2; // Here x is 2 } // Here x is 10 Try it Yourself » · You can learn more about block scope in the chapter JavaScript Scope.
🌐
freeCodeCamp
freecodecamp.org › news › var-let-and-const-whats-the-difference
Var, Let, and Const – What's the Difference?
April 2, 2020 - 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. Variables declared with the const maintain constant values.
🌐
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   November 11, 2021
🌐
W3Schools
w3schools.com › jsref › jsref_let.asp
JavaScript let Statement
break class const continue debugger do...while for for...in for...of function if...else let return switch throw try...catch var while JS Strings
🌐
Educative
educative.io › answers › difference-between-var-let-and-const-keyword-in-javascript
Difference between var, let, and const keyword in JavaScript
The choice between var, let, and const depends on their scoping rules, hoisting behavior, and mutability. JavaScript is known for its flexibility, which allows us to declare variables in different ways.
🌐
W3Schools
w3schools.com › js › js_hoisting.asp
JavaScript Hoisting
Hoisting is JavaScript's default ... the current script or the current function). Variables defined with let and const are hoisted to the top of the block, but not initialized....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › let
let - JavaScript | MDN
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.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_variables.asp
JavaScript Variables
JavaScript variables can hold 8 datatypes, but for now, just think of numbers and strings. Strings are text written inside quotes. Numbers are written without quotes. If you put a number in quotes, it will be treated as a text string. const pi = 3.14; let person = "John Doe"; let answer = 'Yes I am!'; Try it Yourself »
🌐
W3Schools
w3schoolsua.github.io › js › js_const_en.html
JavaScript The const keyword. Examples. Lessons for beginners. W3Schools in English
Redeclaring a JavaScript var variable is allowed anywhere in a program: var x = 2; // Allowed var x = 3; // Allowed x = 4; // Allowed · Redeclaring an existing var or let variable to const, in the same scope, is not allowed:
🌐
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 › @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 ...
🌐
W3Schools
w3schools.com › js › js_es6.asp
JavaScript 2015 (ES6)
Variable can be declared with const, let, or var. iterable - An object that has iterable properties. const cars = ["BMW", "Volvo", "Mini"]; let text = ""; for (let x of cars) { text += x + " "; } Try it Yourself » · let language = "JavaScript"; let text = ""; for (let x of language) { text += x + " "; } Try it Yourself » ·
🌐
Fireship
fireship.dev › var-let-const
var vs let vs const in JavaScript
So to recap, var is function scoped and if you try to use a variable declared with var before the actual declaration, you'll just get undefined. const and let are blocked scoped and if you try to use variable declared with let or const before ...
🌐
Javascript Doctor
javascriptdoctor.blog › home › var vs let vs const explained like you’re 10
Var, Let, and Const: Understanding Variables in JavaScript (Explained Like You're 10!) - Javascript Doctor
3 weeks ago - Use const for things that will never change (like the value of pi). Use let for things that might change later. Avoid using var in modern JavaScript code.
🌐
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.
🌐
W3Schools
w3schools.com › js › js_scope.asp
JavaScript Scope
These two keywords provide Block Scope in JavaScript. Variables declared with let and const inside a code block are "block-scoped," meaning they are only accessible within that block.
🌐
W3Schools Blog
w3schools.blog › home › difference between var and let
Difference between var and let - W3schools
February 27, 2018 - The var statement is used to declare a variable. We can optionally initialize the value of that variable. Example: var num =10; The let statement is used to declare a local variable in a block scope. It is similar to var, in that we can optionally initialize the variable.
🌐
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 - By accessing it before the line of declaration again, we get the cannot access 'number' before initialization reference error · Variables declared with const are similar to let in regards to scope.