Which statement correctly contrasts scope and this?
obj.method() and a plain method() call give different this values, but the variables the function can see never change. Arrow functions take this from their lexical scope too.
Scope and Hoisting · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript scope and hoisting: kinds of scope, the scope chain, lexical scope, hoisting of functions, var, let, const and classes, execution contexts, closures, module scope and globalThis. Pick an option to check your answer instantly.
Which statement correctly contrasts scope and this?
obj.method() and a plain method() call give different this values, but the variables the function can see never change. Arrow functions take this from their lexical scope too.
What is globalThis?
Before ES2020 code had to test for window, self or global. globalThis works in browsers, workers and Node.js.
Inside a function, let total shadows an outer let total. How can the function read the outer one?
Shadowing hides the outer binding completely within the inner scope. (Global var properties could be reached via globalThis, but let/const globals cannot.)
What is the value of save on the first line of a scope that later contains var save = function () {};?
Only the var declaration is hoisted. Calling save() early is therefore a TypeError (save is not a function).
Which is the best way to keep helper variables out of the global scope in modern code?
Modules give every file its own scope. In older code an IIFE did the same job.
Timed online tests with instant scores and explanations.