Advanced JavaScript MCQs for NSCT
Advanced JavaScript covers how JavaScript behaves once code stops being a simple top-to-bottom script: closures, this, promises, async/await and the event loop that decides what runs when. It is topic 7 of the Web Development area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 54.1% accuracy in Web Development, the highest of the ten areas, per the HEC NSCT report and APP.
Revise JavaScript Fundamentals first, since this page assumes you know let, var, functions and scope. Most questions here are "what does this print?", so read every snippet slowly.
Closures
A closure is a function together with the variables of the scope where it was written. The inner function keeps a live reference to those variables, not a copy, even after the outer function has returned.
function makeCounter() {
let count = 0;
return () => ++count;
}
const next = makeCounter();
next(); next();
console.log(next()); // 3
Each call to makeCounter creates a new count, so two counters never share state. In a loop, var gives one shared binding while let gives a fresh binding per iteration, which is why setTimeout inside a var loop prints the final value three times.
Exam angle: expect a loop with setTimeout, or a private-variable "module pattern", and a question on which value each closure sees.
Promises
A promise is an object representing a value that will be available later. It is pending until it settles once, either fulfilled with a value or rejected with a reason. Later calls to resolve or reject are ignored.
.then() returns a new promise, so steps chain. A value returned from a callback passes to the next step, a thrown error skips to the next .catch(), and a .catch() that returns a value recovers the chain.
| Combinator | Settles when | Result |
|---|---|---|
Promise.all |
all fulfil, or the first rejects | array in input order |
Promise.allSettled |
all settle | array of {status, value/reason} |
Promise.race |
the first settles | that outcome, fulfilled or rejected |
Promise.any |
the first fulfils, or all reject | value, or an AggregateError |
Exam angle: forgetting return inside a .then() callback means the chain does not wait for the inner promise.
Async/await
An async function always returns a promise. await pauses that function until the awaited value settles, and the rest of the function then resumes as a microtask. Everything before the first await runs synchronously.
async function f() {
console.log(1);
await null;
console.log(2);
}
f();
console.log(3); // prints 1, 3, 2
Errors from an awaited rejection can be caught with an ordinary try...catch. Watch for return promise inside try: without return await, a rejection escapes the catch.
Exam angle: two independent awaits in a row run one after the other. Starting both and using await Promise.all([...]) lets them run at the same time.
Event loop
JavaScript in a page runs on one thread with one call stack. The event loop takes one task (a timer callback, a click event, a network callback), runs it to completion, then drains the whole microtask queue (promise reactions, queueMicrotask, MutationObserver) before the next task.
console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D'); // A, D, C, B
Exam angle: synchronous code first, then all microtasks, then the next task. A 0 ms timer never beats a promise callback queued in the same script.
this binding
For a regular function, this depends on how it is called: obj.method() sets it to obj, call, apply and bind set it explicitly, and a plain call gives undefined in strict mode. Arrow functions have no this of their own and use the surrounding one, which makes them good callbacks inside methods.
Common traps
- Assuming a closure stores a copy of a variable. It stores a reference, so it sees later changes.
- Thinking
awaiton a non-promise continues synchronously. It always suspends until a microtask. - Using
forEachwith anasynccallback and expecting it to wait. It ignores the returned promises; usefor...oforPromise.all. - Expecting
setTimeout(fn, 0)to run before a.then()callback from the same script. - Passing
obj.methodas a callback and expectingthisto stayobj. Usebindor an arrow wrapper. - Using
constas a classicforloop counter.i++reassigns it and throws aTypeError.
Worked examples
1. Understanding. Which promise method waits for every input to settle and never rejects because one input failed?
Answer: Promise.allSettled. Promise.all rejects on the first failure, race follows the first to settle, and any waits for the first success.
2. Application. What does this print?
let x = 1;
const show = () => console.log(x);
setTimeout(show, 0);
x = 2;
Answer: 2. The arrow function closes over the variable x, not its value at the time it was created. The timer runs after the synchronous assignment x = 2.
3. Analysis. A page logs 'start', then calls fetchA() and fetchB() with await one after the other. Each takes about 500 ms, and neither depends on the other. Why does the page take a full second, and what is the fix?
Answer: the second request does not start until the first await resumes, so the delays add up. Start both promises first and await Promise.all([fetchA(), fetchB()]) to finish in about 500 ms. Making the function non-async would not help, and setTimeout adds delay rather than removing it.
Practise
The quiz below has 100 Advanced JavaScript questions, filterable by tier and difficulty. Every output question was run in Node.js to confirm the answer.
Practise: 100 questions
FAQs
What is the difference between a microtask and a macrotask?
A macrotask, usually just called a task, is a unit of work such as a timer callback, a user event or a script. Microtasks are smaller jobs such as promise reactions. After each task the event loop runs every queued microtask, including any added along the way, before it starts the next task. That is why promise callbacks run before setTimeout callbacks.
Is async/await better than promises?
They are the same mechanism. async/await is syntax built on promises, so an async function returns a promise and await waits for one. It usually reads more clearly and lets you use try...catch. Combinators such as Promise.all are still needed to run independent operations at the same time.
Why do closures cause memory leaks?
They do not by themselves. A closure keeps its outer variables alive only while the closure itself is reachable. Leaks happen when a long-lived object, such as an event listener or a global array, holds a closure that references large data you no longer need. Removing the listener or clearing the reference lets the garbage collector free it.
Is JavaScript single-threaded?
Code on a page runs on one main thread, so only one callback runs at a time. The browser or Node.js does timers, network and file work in the background and queues callbacks for the event loop. Web Workers and Node's worker threads give real parallel threads, each with its own event loop.
Next step
Continue with Frontend Frameworks & Libraries MCQs, drill tricky snippets in JavaScript output questions, go back to the Web Development MCQ hub, or test yourself across every area in an NSCT mock test.