InterviewSolution
| 1. |
What is the difference between let and const? What distinguishes both from var? |
|
Answer» When declaring any variable in JavaScript, we used the var keyword. Var is a function scoped keyword. Within a function, we can access the variable. When we need to CREATE a new scope, we wrap the code in a function. Both LET and const have block scope. If you use these keywords to declare a variable, it will only exist within the innermost block that surrounds them. If you declare a variable with let INSIDE a block (for EXAMPLE, if a condition or a for-loop), it can only be accessed within that block. The variables declared with the let keyword are mutable, which means that their values can be changed. It's akin to the var keyword, but with the added BENEFIT of block scoping. The variables declared with the const keyword are block-scoped and immutable. When variables are declared with the const keyword, their value cannot be modified or reassigned. |
|