ES6+ & Modern JavaScript — Interview Questions
🟡 ES6+ & Modern JavaScript
Section titled “🟡 ES6+ & Modern JavaScript”Q1. What are template literals?
Section titled “Q1. What are template literals?”Answer: Template literals use backticks and support embedded expressions and multi-line strings.
const name = "Alice";const age = 30;
// String interpolationconsole.log(`My name is ${name} and I am ${age} years old.`);
// Multi-lineconst msg = `Line 1Line 2`;
// Expressionconsole.log(`2 + 2 = ${2 + 2}`); // "2 + 2 = 4"
// Tagged templatesfunction tag(strings, ...values) { return strings[0] + values[0].toUpperCase();}tag`Hello ${"world"}`; // "Hello WORLD"Q2. What is destructuring in JavaScript?
Section titled “Q2. What is destructuring in JavaScript?”Answer:
// Array Destructuringconst [a, b, c] = [1, 2, 3];const [first, , third] = [10, 20, 30]; // skip elements
// Object Destructuringconst { name, age } = { name: "Alice", age: 30 };
// Renamingconst { name: userName } = { name: "Bob" };
// Default valuesconst { x = 10, y = 20 } = { x: 5 };console.log(x, y); // 5, 20
// Nestedconst { address: { city } } = { address: { city: "Mumbai" } };Q3. What is the spread (...) and rest (...) operator?
Section titled “Q3. What is the spread (...) and rest (...) operator?”Answer:
// Spread — expands elementsconst arr1 = [1, 2, 3];const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
const obj1 = { a: 1 };const obj2 = { ...obj1, b: 2 }; // { a: 1, b: 2 }
// Rest — collects remaining elementsfunction sum(...nums) { return nums.reduce((a, b) => a + b, 0);}sum(1, 2, 3, 4); // 10
const [head, ...tail] = [1, 2, 3, 4];// head = 1, tail = [2, 3, 4]Q4. What are default parameters?
Section titled “Q4. What are default parameters?”Answer:
function greet(name = "World") { return `Hello, ${name}!`;}
greet(); // "Hello, World!"greet("Alice"); // "Hello, Alice!"
// Works with expressionsfunction add(a, b = a * 2) { return a + b;}add(5); // 15Q5. What is optional chaining (?.)?
Section titled “Q5. What is optional chaining (?.)?”Answer:
const user = { profile: { name: "Alice" } };
// Without optional chaining — can throwuser.address.city; // ❌ TypeError
// With optional chaining — returns undefineduser?.address?.city; // undefined ✅
// With methodsuser?.getAge?.(); // undefined if method doesn't exist
// With arraysuser?.friends?.[0]; // undefined if friends doesn't existQ6. What is the nullish coalescing operator (??)?
Section titled “Q6. What is the nullish coalescing operator (??)?”Answer:
// Returns right side only if left is null or undefinednull ?? "default" // "default"undefined ?? "default" // "default"0 ?? "default" // 0 ← 0 is NOT null/undefined"" ?? "default" // "" ← "" is NOT null/undefinedfalse ?? "default" // false
// vs || which triggers on any falsy0 || "default" // "default" ← 0 is falsy"" || "default" // "default" ← "" is falsyQ7. What is Symbol in JavaScript?
Section titled “Q7. What is Symbol in JavaScript?”Answer:
// Symbol creates a unique identifierconst id1 = Symbol("id");const id2 = Symbol("id");console.log(id1 === id2); // false — always unique
// Use case: unique object keys (avoid property collisions)const key = Symbol("secretKey");const obj = { [key]: "hidden value" };obj[key]; // "hidden value"
// Symbols are NOT enumerableObject.keys(obj); // [] — symbol keys excludedObject.getOwnPropertySymbols(obj); // [Symbol(secretKey)]
// Well-known symbolsSymbol.iteratorSymbol.toPrimitiveSymbol.hasInstanceQ8. What is Proxy in JavaScript?
Section titled “Q8. What is Proxy in JavaScript?”Answer:
const handler = { get(target, key) { console.log(`Getting ${key}`); return key in target ? target[key] : `Property "${key}" not found`; }, set(target, key, value) { if (typeof value !== "number") throw new TypeError("Only numbers!"); target[key] = value; return true; }};
const obj = new Proxy({}, handler);obj.age = 30; // ✅obj.age; // logs "Getting age", returns 30obj.name = "Bob"; // ❌ TypeError: Only numbers!
// Use cases: validation, logging, reactive data (Vue 3 uses Proxy!)Q9. What is Reflect in JavaScript?
Section titled “Q9. What is Reflect in JavaScript?”Answer:
// Reflect mirrors all Proxy traps as functionsconst obj = { x: 1 };
Reflect.get(obj, "x"); // 1Reflect.set(obj, "y", 2); // trueReflect.has(obj, "x"); // trueReflect.deleteProperty(obj, "x"); // trueReflect.ownKeys(obj); // ["y"]
// Common with Proxy — forward default behaviorconst handler = { set(target, key, value) { console.log(`Setting ${key} = ${value}`); return Reflect.set(target, key, value); // default behavior }};Q10. What are tagged template literals?
Section titled “Q10. What are tagged template literals?”Answer:
function highlight(strings, ...values) { return strings.reduce((result, str, i) => { return result + str + (values[i] ? `<mark>${values[i]}</mark>` : ""); }, "");}
const name = "Alice";const score = 95;highlight`Player ${name} scored ${score} points!`;// "Player <mark>Alice</mark> scored <mark>95</mark> points!"
// Real use cases:// - css`` (styled-components)// - sql`` (SQL template literals)// - html`` (sanitizing HTML)// - gql`` (GraphQL queries)Q11. What are JavaScript modules (ESM)?
Section titled “Q11. What are JavaScript modules (ESM)?”Answer:
// math.js — named exportsexport const PI = 3.14159;export function add(a, b) { return a + b; }
// Default exportexport default class Calculator { /* ... */ }
// main.js — importsimport Calculator, { PI, add } from "./math.js";import * as math from "./math.js"; // namespace import
// Dynamic importconst module = await import("./heavy.js"); // lazy loading
// Re-exportexport { add, subtract } from "./math.js";export * from "./math.js";Q12. What is the difference between CommonJS and ES Modules?
Section titled “Q12. What is the difference between CommonJS and ES Modules?”Answer:
// CommonJS (Node.js default)const fs = require("fs");module.exports = { add, subtract };
// ES Modulesimport fs from "fs";export { add, subtract };
// Key differences:// CJS: synchronous, loads at runtime, dynamic require() allowed// ESM: asynchronous, static analysis at parse time, tree-shakeable
// CJS: exports is an object (copy), ESM: live bindings// CJS: works in Node.js out of the box// ESM: requires .mjs extension or "type": "module" in package.json// ESM: supports top-level awaitQ13. What are WeakRef and FinalizationRegistry?
Section titled “Q13. What are WeakRef and FinalizationRegistry?”Answer:
// WeakRef — hold reference without preventing GClet obj = { name: "Alice" };const weakRef = new WeakRef(obj);weakRef.deref()?.name; // "Alice"obj = null; // obj can now be GC'd
// FinalizationRegistry — run cleanup when object is GC'dconst registry = new FinalizationRegistry((value) => { console.log(`${value} was garbage collected`);});
let target = { data: "important" };registry.register(target, "target object");target = null; // When GC runs: "target object was garbage collected"Q14. What is setTimeout and setInterval?
Section titled “Q14. What is setTimeout and setInterval?”Answer:
// setTimeout — runs once after delay (ms)const timerId = setTimeout(() => { console.log("Runs after 2 seconds");}, 2000);clearTimeout(timerId); // cancel before it runs
// setInterval — runs repeatedly every intervalconst intervalId = setInterval(() => { console.log("Runs every second");}, 1000);clearInterval(intervalId); // stop itQ15. What is the Temporal API (Stage 3)?
Section titled “Q15. What is the Temporal API (Stage 3)?”Answer:
// Temporal replaces the broken Date API
// Current dateconst today = Temporal.Now.plainDateISO();
// Date arithmetic (immutable!)const nextWeek = today.add({ days: 7 });const lastMonth = today.subtract({ months: 1 });
// Durationconst duration = Temporal.Duration.from({ days: 30, hours: 2 });today.add(duration);
// Timezone-awareconst now = Temporal.Now.zonedDateTimeISO("Asia/Kolkata");
// Difference between datesconst start = Temporal.PlainDate.from("2024-01-01");const end = Temporal.PlainDate.from("2024-12-31");start.until(end); // Duration { days: 365 }
// No mutation, no timezone bugs, no month indexing confusion!Q16. What are the latest JavaScript features (ES2023/ES2024)?
Section titled “Q16. What are the latest JavaScript features (ES2023/ES2024)?”Answer:
// ES2023// Array.findLast / findLastIndex[1,2,3,4,5].findLast(n => n % 2 === 0); // 4[1,2,3,4,5].findLastIndex(n => n < 4); // 2
// Array.toSorted / toReversed / toSpliced / with (non-mutating!)const arr = [3, 1, 2];arr.toSorted(); // [1, 2, 3] — original unchangedarr.toReversed(); // [2, 1, 3] — original unchangedarr.with(1, 99); // [3, 99, 2] — original unchanged
// ES2024// Object.groupBy / Map.groupByconst people = [{name:"Alice", age:25}, {name:"Bob", age:17}];Object.groupBy(people, p => p.age >= 18 ? "adults" : "minors");// { adults: [{Alice}], minors: [{Bob}] }
// Promise.withResolversconst { promise, resolve, reject } = Promise.withResolvers();resolve("done");await promise; // "done"
// Array.fromAsync (ES2024)const arr2 = await Array.fromAsync(asyncGenerator());Q17. What is AggregateError and when is it used?
Section titled “Q17. What is AggregateError and when is it used?”Answer:
// AggregateError — holds multiple errors// Used by Promise.any() when ALL promises reject
const p1 = Promise.reject(new Error("error 1"));const p2 = Promise.reject(new Error("error 2"));const p3 = Promise.reject(new Error("error 3"));
Promise.any([p1, p2, p3]).catch(err => { console.log(err instanceof AggregateError); // true console.log(err.message); // "All promises were rejected" console.log(err.errors); // [Error: error 1, Error: error 2, Error: error 3]});
// Throw it manuallythrow new AggregateError( [new Error("a"), new Error("b")], "Multiple errors occurred");Q18. What is the AbortController?
Section titled “Q18. What is the AbortController?”Answer:
// Cancel fetch requestsconst controller = new AbortController();const signal = controller.signal;
fetch("/api/data", { signal }) .then(res => res.json()) .then(data => console.log(data)) .catch(err => { if (err.name === "AbortError") { console.log("Request was cancelled"); } });
// Cancel after 5 secondssetTimeout(() => controller.abort(), 5000);
// Real use: cancel stale search requestslet currentController;input.addEventListener("input", async (e) => { currentController?.abort(); // cancel previous currentController = new AbortController(); const results = await fetch(`/search?q=${e.target.value}`, { signal: currentController.signal });});Q19. What is queueMicrotask?
Section titled “Q19. What is queueMicrotask?”Answer:
// Schedule a microtask (higher priority than setTimeout)console.log("1");
queueMicrotask(() => console.log("2 - microtask"));
Promise.resolve().then(() => console.log("3 - promise"));
setTimeout(() => console.log("4 - setTimeout"), 0);
console.log("5");
// Output: 1 → 5 → 2 → 3 → 4// (microtasks run before macrotasks in same cycle)Q20. What is TypedArray and ArrayBuffer?
Section titled “Q20. What is TypedArray and ArrayBuffer?”Answer:
// ArrayBuffer — fixed-size raw binary dataconst buffer = new ArrayBuffer(16); // 16 bytes
// TypedArray — view over ArrayBuffer with a specific typeconst int32 = new Int32Array(buffer); // 4 ints (4 bytes each)const float64 = new Float64Array(buffer); // 2 doubles (8 bytes each)const uint8 = new Uint8Array(buffer); // 16 bytes
// Writeint32[0] = 42;uint8[0]; // access same memory, different view
// DataView — fine-grained control over byte orderconst view = new DataView(buffer);view.setInt32(0, 42, true); // true = little-endianview.getInt32(0, false); // big-endian read
// Use cases: WebGL, Web Audio, WebSockets binary, WASMconst pixels = new Uint8ClampedArray(width * height * 4); // RGBA