![]() Still, they could be asked.Īnyways, many of these questions can truly assess if you are seasoned in JavaScript, like the tricky closure. I have the same feeling, especially regarding the eagle eye test. You can think that some of the questions are useless for interviewing. const variables are in a temporal dead zone until the declaration line const m圜onst = 3.14.įollow the guide JavaScript Variables Hoisting in Details to get a good grasp on hoisting. ![]() However, accessing m圜onst before the declaration line throws a ReferenceError. A hoisted var variable, before its initialization, has an undefined value. Hoisting and temporal dead zone are 2 important concepts that influence the lifecycle of JavaScript variables.Īccessing myVar before declaration evaluates to undefined. If you struggle understanding closures, I recommend reading A Simple Explanation of JavaScript Closures.ĭo you know how to fix the snippet to log 0, 1, and 2? Please write your solution in a comment below! 6. That's why the output to the console is 3, 3 and 3. log() reads the current value of variable i, which is 3, and logs to console 3. The 3 scheduled log() callbacks are called by setTimeout().It's important to understand that the closure captures i variable lexically. ![]() Log() is a closure that captures the variable i, which is defined in the outside scope of for() cycle.
0 Comments
Leave a Reply. |