A closure is a function bundled together with references to its surrounding state (lexical environment). Closures are used for data encapsulation, factory functions, and callbacks. Example: function counter() { let n=0; return () => ++n; }
2 / 5
Complete the sentence: "Instead of using callbacks, the team refactored the code to use _____ with async/await, making the error handling much cleaner."
Promises represent the eventual result (or failure) of an asynchronous operation. async/await is syntactic sugar over Promises — an async function always returns a Promise, and await pauses execution until the Promise settles.
3 / 5
In TypeScript, type narrowing means:
Type narrowing is when TypeScript refines a broader type to a more specific one inside a conditional block. For example, after if (typeof x === "string"), TypeScript knows x is a string inside that block. Common narrowing guards: typeof, instanceof, in, and custom type predicates (x is T).
4 / 5
TypeScript generics are used to:
Generics let you write flexible, reusable components that work with different types without losing type safety. Example: function identity<T>(arg: T): T { return arg; }. Common in utility types like Array<T>, Promise<T>, Record<K, V>.
5 / 5
"We can use destructuring to extract just the fields we need." Which of the following is an example of object destructuring?
Destructuring is a concise syntax to unpack values from objects or arrays into variables. Object destructuring: const { name, role } = user. Array destructuring: const [first, second] = arr. You can also rename: const { name: userName } = user and set defaults: const { role = "guest" } = user.