JavaScript Fundamentals MCQs for the NSCT
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.
vardeclarations are hoisted and set toundefined; the assignment stays in place.- Function declarations are hoisted with their body, so they can be called early.
let,constandclassare hoisted but left uninitialised. The stretch before the declaration is the temporal dead zone (TDZ), where any access, eventypeof, throws a ReferenceError.- A function expression stored in
varis onlyundefinedearly, so calling it throws a TypeError.
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
- Reading
typeof nullas"null". It is"object". - Calling
[10, 9, 1].sort()numeric. Without a compare function it sorts as strings. - Expecting a ReferenceError for
varused early. It isundefined. - Treating
3 > 2 > 1as a range check. It becomestrue > 1, which is false. - Writing
if (x = 5). That assigns and is always truthy. - Querying the DOM from a script in
<head>withoutdefer, so the element is stillnull.
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
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.