JavaScript Fundamentals MCQs for the NSCT

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

JavaScript fundamentals means the core rules of the language that run in every browser: its types and how they convert, how scope decides where a variable is visible, how hoisting moves declarations, and how scripts read and change the page through the DOM. It is the sixth topic in the Web Development area, which carries 10% of the NSCT according to HEC's Areas of Competencies.

Many questions show a few lines of code and ask what they print. Every code answer in our quiz was run in Node.js to confirm the output.

Types

JavaScript is dynamically typed: values have types, variables do not. There are seven primitive types (string, number, bigint, boolean, undefined, symbol and null) and one structural type, object, which includes arrays and functions.

Expression Result
typeof null "object" (a historical bug)
typeof [] "object"; use Array.isArray([])
typeof function () {} "function"
"5" + 3 "53" (+ concatenates if either side is a string)
"5" - 3 2 (− always converts to numbers)
NaN === NaN false; use Number.isNaN()

Loose equality (==) converts types before comparing; strict equality (===) does not. So "" == 0 is true but "" === 0 is false. The falsy values besides false are 0 (and -0), "", null, undefined, NaN and 0n; almost everything else, including [] and "0", is truthy.

Exam angle: work through + left to right. 1 + "2" - 1 is "12" - 1, which is 11.

Scope

Scope is the region of code where a name can be used. JavaScript has global, function and block scope.

var let const
Scope Function Block Block
Reassign Yes Yes No
Redeclare in same scope Yes No No
Before declaration undefined ReferenceError ReferenceError
function demo() {
  if (true) { var v = 1; let w = 2; }
  return typeof v + " " + typeof w; // "number undefined"
}

const stops reassignment, not mutation: const o = {a: 1}; o.a = 2; is allowed. A variable declared in an inner scope with the same name shadows the outer one.

Exam angle: var in a loop is shared by every iteration, which is why callbacks created in a for (var i ...) loop all see the final value.

Hoisting

Hoisting is how declarations are processed when a scope is created, before any code in it runs, so they behave as if moved to the top.

Exam angle: decide the error type. Using an undeclared name or a TDZ variable gives ReferenceError; calling something that is undefined gives TypeError.

DOM

The Document Object Model (DOM) is the browser's tree of objects representing the page. Scripts find nodes, change them and react to events:

const list = document.querySelector("#tasks");
const li = document.createElement("li");
li.textContent = "Revise CSS";
list.appendChild(li);
list.addEventListener("click", (e) => e.target.classList.toggle("done"));
Method Returns
getElementById("x") One element or null
querySelector(".x") First match or null
querySelectorAll(".x") Static NodeList
getElementsByClassName("x") Live HTMLCollection

Events bubble from the target up to its ancestors by default, which makes event delegation, one listener on a parent, possible.

Exam angle: textContent inserts plain text; innerHTML parses HTML and can open a cross-site scripting (XSS) hole with user input.

Common traps

Worked examples

1. Understanding. What does console.log(typeof [], Array.isArray([])) print? Answer: object true. Arrays are objects, so typeof cannot tell them apart; Array.isArray can.

2. Application. What does console.log(1 + "2" - 1) print? Answer: 11. 1 + "2" concatenates to "12", then - converts it back to the number 12, and 12 − 1 = 11.

3. Analysis. What does this print?

let total = 0;
function add() {
  if (false) { var total = 5; }
  total += 1;
  return total;
}
console.log(add(), total);

Answer: NaN 0. The var total inside add is hoisted to the top of the function even though its block never runs, so it shadows the outer total and starts as undefined. undefined + 1 is NaN, and the outer total stays 0.

Practise

The quiz below has 99 questions on types, scope, hoisting and the DOM, filterable by tier and difficulty.

Practise: 99 questions

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

  1. What does this code print?

    console.log(typeof null);
    1. null
    2. object
    3. undefined
    4. number
    Show answer

    Answer: B. object

    typeof null returns "object". This is a long-standing bug from the first version of JavaScript that was kept for compatibility; null is still a primitive value. Test for it with value === null.

  2. Which keyword declares a block-scoped variable that can be reassigned later?

    1. const
    2. var
    3. static
    4. let
    Show answer

    Answer: D. let

    let creates a block-scoped binding that can be reassigned. const is also block-scoped but cannot be reassigned. var is function-scoped, and static is used in classes, not for variable declarations.

  3. Which of these is NOT a primitive type in JavaScript?

    1. Array
    2. string
    3. boolean
    4. symbol
    Show answer

    Answer: A. Array

    JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol and null. Arrays are objects: typeof [] returns "object".

  4. How does === differ from ==?

    1. === compares only numbers; == compares any type
    2. === assigns a value; == compares
    3. === compares without type conversion; == converts types before comparing
    4. They are identical in modern JavaScript
    Show answer

    Answer: C. === compares without type conversion; == converts types before comparing

    Strict equality (===) is true only when both type and value match. Loose equality (==) first coerces the operands, so "5" == 5 is true but "5" === 5 is false.

  5. What does this code print?

    const o = { a: 1 };
    o.a = 2;
    console.log(o.a);
    1. 1
    2. TypeError: Assignment to constant variable.
    3. 2
    4. undefined
    Show answer

    Answer: C. 2

    const stops the variable from being reassigned to a new value, but the object it points to can still change. Setting o.a = 2 mutates the object, so it prints 2. Writing o = {} would throw a TypeError.

  6. What is the temporal dead zone (TDZ)?

    1. The part of a scope before a let or const declaration runs, where using the variable throws a ReferenceError
    2. The time a var variable holds undefined
    3. Code that runs after a function returns
    4. A variable that the garbage collector has removed
    Show answer

    Answer: A. The part of a scope before a let or const declaration runs, where using the variable throws a ReferenceError

    let and const bindings are hoisted to the top of their block but stay uninitialised until the declaration line runs. Reading them in that zone throws a ReferenceError, unlike var, which reads as undefined.

  7. Which statement about hoisting is correct?

    1. Function expressions assigned to var can be called before their definition
    2. let variables are not hoisted at all
    3. Function declarations are hoisted with their body, so they can be called before the line that defines them
    4. Only arrow functions are hoisted
    Show answer

    Answer: C. Function declarations are hoisted with their body, so they can be called before the line that defines them

    A function declaration is created when its scope is set up, body included. With var f = function(){}, only the variable is hoisted (as undefined), so calling it early throws a TypeError. let and const are hoisted too, but sit in the temporal dead zone.

  8. What does document.getElementById("menu") return if no element has that id?

    1. undefined
    2. null
    3. An empty NodeList
    4. It throws an error
    Show answer

    Answer: B. null

    getElementById returns the matching Element or null. Methods such as querySelectorAll return an empty NodeList instead, and none of them throw when nothing matches.

  9. How does document.querySelectorAll(".item") differ from document.getElementsByClassName("item")?

    1. querySelectorAll returns a static NodeList; getElementsByClassName returns a live HTMLCollection
    2. querySelectorAll returns a live list; getElementsByClassName returns a static list
    3. querySelectorAll returns only the first match
    4. getElementsByClassName accepts any CSS selector
    Show answer

    Answer: A. querySelectorAll returns a static NodeList; getElementsByClassName returns a live HTMLCollection

    A static NodeList is a snapshot: adding a new .item later does not change it. A live HTMLCollection updates automatically as the document changes. querySelector returns only the first match, and only the query methods accept full CSS selectors.

  10. Why is el.textContent = userInput safer than el.innerHTML = userInput?

    1. textContent inserts plain text, while innerHTML parses the string as HTML and can run injected markup
    2. textContent is faster but otherwise identical
    3. innerHTML only works on div elements
    4. textContent escapes CSS but not HTML
    Show answer

    Answer: A. textContent inserts plain text, while innerHTML parses the string as HTML and can run injected markup

    innerHTML parses its value as HTML, so input like <img src=x onerror=alert(1)> creates elements and runs handlers: a cross-site scripting (XSS) risk. textContent treats the value as literal text.

FAQs

What is the difference between null and undefined?

undefined means a value was never assigned: an unset variable, a missing property or a function with no return. null is a value a programmer assigns on purpose to mean "no object". They are loosely equal (null == undefined is true) but not strictly equal, and typeof gives "object" for null and "undefined" for undefined.

Should I still use var?

In new code, no. let and const are block-scoped and throw on early access, which catches bugs that var hides. Use const by default and let when you must reassign. You still need to understand var because older code and exam questions use it.

Are closures and promises part of this topic?

They are covered in Advanced JavaScript, along with async/await and the event loop. This topic covers the base rules those features rely on. For mixed print-the-output drills, try JavaScript output questions.

Is the DOM part of JavaScript?

Not strictly. The DOM is a web API that browsers provide, and JavaScript is the language used to work with it. Node.js runs JavaScript without a DOM, which is why document is undefined there.

Next step

Move on to Advanced JavaScript, go back to the Web Development MCQs hub, or test yourself in an NSCT mock test.