Advanced JavaScript MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. Which statement best defines a closure in JavaScript?

    1. A copy of the outer function's variables taken at the moment the inner function is created
    2. A function together with the variables of the scope it was defined in, which it can still use after that outer function returns
    3. A function that calls itself until it reaches a base case
    4. A function that is defined and invoked immediately in one expression
    Show answer

    Answer: B. A function together with the variables of the scope it was defined in, which it can still use after that outer function returns

    A closure is a function plus its lexical environment. The inner function keeps a live reference to the outer variables, not a snapshot, so it can read and change them even after the outer function has returned. Recursion and IIFEs are separate ideas.

  2. Which three states can a JavaScript Promise be in?

    1. Waiting, running and completed
    2. Pending, resolved, failed and cancelled
    3. Created, executing and settled
    4. Pending, fulfilled and rejected
    Show answer

    Answer: D. Pending, fulfilled and rejected

    A Promise starts pending and settles once, either fulfilled with a value or rejected with a reason. 'Settled' describes being fulfilled or rejected, not a third state, and native promises cannot be cancelled.

  3. What does a function declared with the async keyword always return when it is called?

    1. A Promise
    2. The value in its return statement, unchanged
    3. undefined until every await has finished
    4. A generator object
    Show answer

    Answer: A. A Promise

    Calling an async function always returns a Promise. A returned value fulfils that promise, and a thrown error rejects it. Callers get the value through await or .then(), never directly.

  4. When a Promise is fulfilled, where are its .then() callbacks placed before they run?

    1. In the task (macrotask) queue, behind setTimeout callbacks
    2. Straight onto the call stack, so they run immediately
    3. In a separate worker thread
    4. In the microtask queue
    Show answer

    Answer: D. In the microtask queue

    Promise reactions are queued as microtasks. The event loop drains the whole microtask queue as soon as the call stack is empty, before it takes the next task such as a setTimeout callback. They never interrupt code that is already running.

  5. According to the event loop model used by browsers and Node.js, when are queued microtasks processed?

    1. Only after every pending setTimeout callback has run
    2. One microtask per loop iteration, alternating with tasks
    3. In parallel with the main thread whenever a CPU core is free
    4. After the current task finishes and the call stack is empty, all of them, before the next task starts
    Show answer

    Answer: D. After the current task finishes and the call stack is empty, all of them, before the next task starts

    After each task the engine runs a microtask checkpoint: it keeps taking microtasks, including ones queued during the checkpoint, until the queue is empty. Only then does the loop pick the next task. JavaScript callbacks never run in parallel on one thread.

  6. Where can the await keyword be used?

    1. Anywhere in any script, including ordinary functions
    2. Only inside .then() callbacks
    3. Inside an async function, and at the top level of an ES module
    4. Only inside generator functions
    Show answer

    Answer: C. Inside an async function, and at the top level of an ES module

    await is valid in async functions and, since ES2022, at the top level of ES modules. Inside an ordinary function or a classic script it is a syntax error. Generators use yield, not await.

  7. Which statement about Promise.all() is correct?

    1. It fulfils with the results in the order the promises finished
    2. It always waits for every promise and reports each one's outcome
    3. It fulfils with the results in input order once all fulfil, and rejects as soon as any one rejects
    4. It fulfils with the value of the first promise that fulfils
    Show answer

    Answer: C. It fulfils with the results in input order once all fulfil, and rejects as soon as any one rejects

    Promise.all keeps results in the same order as the input array, whatever order they finish in. One rejection rejects the whole thing immediately. Waiting for every outcome is Promise.allSettled, and first-to-fulfil is Promise.any.

  8. Which Promise combinator settles as soon as the first input promise settles, whether it fulfils or rejects?

    1. Promise.any()
    2. Promise.race()
    3. Promise.allSettled()
    4. Promise.all()
    Show answer

    Answer: B. Promise.race()

    Promise.race copies the outcome of whichever promise settles first, fulfilled or rejected. Promise.any ignores rejections and waits for the first fulfilment, allSettled waits for all of them, and all waits for all fulfilments or the first rejection.

  9. How is the value of this decided inside an arrow function?

    1. It is taken from the enclosing lexical scope where the arrow function is written
    2. It is the object before the dot when the arrow function is called
    3. It is always the global object
    4. It is the new object when the arrow function is called with new
    Show answer

    Answer: A. It is taken from the enclosing lexical scope where the arrow function is written

    Arrow functions have no this binding of their own, so they use this from the surrounding code. That is why they are handy as callbacks inside methods. They cannot be used as constructors, and call, apply and bind cannot change their this.

  10. Why do closures created inside a for (let i = 0; ...) loop each see a different value of i?

    1. let copies the value of i into each closure when the closure runs
    2. Closures capture values rather than variables when let is used
    3. The loop runs each iteration on a separate thread
    4. let creates a new binding of i for every loop iteration
    Show answer

    Answer: D. let creates a new binding of i for every loop iteration

    With let in a for header, the specification creates a fresh binding per iteration and copies the current value into it. Each closure captures its own binding. With var there is a single function-scoped binding that all closures share.

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.