What is the use of var let & const in javascript with an example program?
The Parameters
The names of the variable or variables to declare. Each must be a legal JavaScript identifier. valueN OptionalFor each variable declared, you may optionally specify its initial value to any legal JavaScript expression. Alternatively, the Destructuring Assignment syntax can also be used to declare variables.
Description Just like An explanation of why the name "let" was chosen can be found here. Many issues with Unlike
ExamplesScoping rules Variables declared by
At the top level of programs and functions,
RedeclarationsRedeclaring the same variable within the same function or block scope raises a
You may
encounter errors in
However, it's important to point out that a block nested inside a case clause will create a new block scoped lexical environment, which will not produce the redeclaration errors shown above.
Temporal dead zone (TDZ)A While inside the TDZ, the variable has not been
initialized with a value, and any attempt to access it will result in a This differs from
The term "temporal" is used because the zone depends on the order of execution (time) rather than the
order in which the code is written (position). For example, the code below works because, even though the function that uses the
The TDZ and |
Specification |
---|
ECMAScript Language Specification # sec-let-and-const-declarations |
Browser compatibility
BCD tables only load in the browser