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 OverflowW3Schools
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.
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.
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:
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 » ·
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.