What is the difference between using “let” and “var”?
Announcement
Collapse
No announcement yet.
What is the difference between using “let” and “var”?
Collapse
X
-
Variables declared by the "var" keyword are scoped to the immediate function body (function scope) while "let" variables are scoped to the immediate enclosing block denoted by {....} (block scope).
Variable declared with var keyword can be re-declared and updated in the same scope while variable declared with let keyword can be updated but not re-declared.
Variables defined with let are hoisted to the top of the block, but not initialized. This means that the block of code is aware of the variable, but it cannot be used until it has been declared. So, using a let variable before it is declared will result in a ReferenceError.
- Likes 2
Related Topics
Collapse
-
by MohanWhat is the difference between client-side and server-side programming?
...-
Channel: Javascript
08-24-2021, 11:30 AM -
-
by JavaScriptWhat is the difference between let and var
-
Channel: Javascript Interview questions
08-28-2021, 11:52 AM -
-
by JavaScriptWhat is the difference between document load and DOMContentLoaded events
-
Channel: Javascript Interview questions
08-28-2021, 12:53 PM -
-
by JavaScriptWhat is the main difference between Object.values and Object.entries method
The Object.values() method's behavior is similar...-
Channel: Javascript Interview questions
08-28-2021, 01:51 PM -
-
by JavaScriptWhat is the main difference between localStorage and sessionStorage
-
Channel: Javascript Interview questions
08-28-2021, 12:14 PM -
Comment