“Let”, “Var” and Scoping
Q: Can you explain the difference between “Let” and “Var”?
A: Yes. I can cite three differences.
First, let has block scoping. In contrast, var has functional scoping in Js.
Second, we can re-declare the reassigned variable with var. In let, however, we cannot re-declare the reassigned value.
The last thing I can think of is that var directly assigns its global variable to window object. On the other hand, let gets assigned to an invisible declarative environment that works similar to global.
The Difference of “Let” and “Const”
Q: Can you discuss the difference between the keywords “let” and “const”?
A: “Let” and “const” are both used to describe a variable.
Both are signals. Let signals that the variable may be reassigned. It may be through a counter loop or a value swap in an algorithm. It signals that the variable can only be used in the block from which it is defined in which is not most of the time the entire containing function.
In contrast, Const signals that the identifier will not be reassigned.
A: It simplifies the function and thus would consequently save the developers time. It is also known to be the most popular feature of ES6. Arrow functions can also be called fat arrow functions. It uses a new token, =>, thus, the name fat arrow. They work like a one-line mini function, and by utilizing such, we can avoid using the function keyword, return keyword and curly brackets. Using this function helps our work by making the code more concise. It also simplifies our function scoping.
Function Declaration and Function Expression
Q: Can you discuss the difference between function declaration and function expression?
A: The main difference between the two is the time of which each load. If it loads before any code is executed, it is a Function Declaration. But if it loads when the interpreter reaches that line of code, it would basically be a function expression.
Another notable distinction is that function declarations are raised to the top of the other code. In contrast, function expressions are not raised. This allows the latter to retain the copies of the local variables coming from the scope from where they were defined.
A: The combination of a function bundled together or enclosed in relation to its surrounding state or lexical environment is called a closure. To put it in simpler words, a closure can give access to an outer function’s scope from an inner function. At every function creation time, wherein a function is created, a closure is also consequently being created.
Closures have practical uses since they can let you associate data with another function that operates on that data. There can be comparisons with object-oriented programming, wherein such objects allow the programmer to associate data with one or more methods; as a result, there is closure.
SetTimeout and SetInterval;
The only notable distinction between the two is that in setTimeout(), it triggers the expression only once. In contrast, setInterval continuously triggers the expression regularly after the given interval of time.