Episode 1 — Fundamentals / 1.18 — Operators and Type System
1.18 — Exercise Questions: Operators and the Type System
Practice questions for all seven subtopics in Section 1.18. Mix of code prediction, short-answer, debugging, and reasoning tasks.
How to use this material (instructions)
- Read lessons in order —
README.md, then1.18.a→1.18.g. - Answer closed-book first — then compare to the matching lesson.
- Use DevTools Console — paste code snippets to verify predictions.
- Interview prep —
1.18-Interview-Questions.md. - Quick review —
1.18-Quick-Revision.md.
1.18.a — Arithmetic Operators (Q1–Q10)
Q1. What is the result of 10 % 3? What does the % operator return?
Q2. Predict the output:
let x = 5;
console.log(x++);
console.log(x);
console.log(++x);
console.log(x);
Q3. What is 2 ** 3 ** 2? Explain why (hint: associativity).
Q4. Predict:
console.log(1 + 2 + "3");
console.log("1" + 2 + 3);
Q5. What does 0 / 0 return? What about 1 / 0?
Q6. Why does 0.1 + 0.2 !== 0.3? How would you fix this comparison?
Q7. Predict:
console.log(typeof NaN);
console.log(NaN === NaN);
Q8. Write a function isEven(n) using the % operator.
Q9. What is the result of "hello" * 2?
Q10. Predict:
console.log(2 + 3 * 4);
console.log((2 + 3) * 4);
console.log(2 ** 3 * 2);
1.18.b — Comparison Operators (Q11–Q20)
Q11. What is the difference between == and ===?
Q12. Predict the output of each:
console.log(5 == "5");
console.log(5 === "5");
console.log(0 == false);
console.log(0 === false);
console.log(null == undefined);
console.log(null === undefined);
Q13. Why does "" == false return true? Walk through the coercion steps.
Q14. Predict:
console.log(NaN === NaN);
console.log(NaN == NaN);
console.log(NaN !== NaN);
Q15. What are the results of these three expressions?
console.log(null > 0);
console.log(null == 0);
console.log(null >= 0);
Explain the inconsistency.
Q16. Predict:
console.log("10" < "9");
console.log("10" < 9);
Why are they different?
Q17. How does Object.is() differ from ===? Give two examples.
Q18. What does [] == false return? Walk through the coercion steps.
Q19. Write a comparison that checks if a variable is null or undefined using a single ==.
Q20. Debug this code: The function should return true only for the number 0, but it has a bug:
function isZero(val) {
return val == 0;
}
console.log(isZero(0)); // true (correct)
console.log(isZero("")); // ? (should be false)
console.log(isZero(false)); // ? (should be false)
1.18.c — Logical Operators (Q21–Q32)
Q21. Predict the output:
console.log(true && "hello");
console.log(false && "hello");
console.log("a" && "b" && "c");
console.log("a" && 0 && "c");
Q22. Predict:
console.log(false || "hello");
console.log("" || "default");
console.log(0 || null || "found");
console.log("first" || "second");
Q23. What is the result of !!"hello"? What about !!0? What about !![]?
Q24. What is the difference between || and ??? Give an example where they produce different results.
Q25. Predict:
console.log(0 || 42);
console.log(0 ?? 42);
console.log("" || "default");
console.log("" ?? "default");
Q26. Why does this code fail?
const result = null || undefined ?? "default";
Q27. Predict:
console.log(!true || false);
console.log(!(true || false));
console.log(true || false && false);
Q28. What does short-circuit evaluation mean? Write code that demonstrates && short-circuiting.
Q29. Rewrite this using ?? and optional chaining:
const city = user && user.address && user.address.city || "Unknown";
Q30. What is the output?
let x = 0;
const result = x || 10;
console.log(result);
Is this always the intended behavior?
Q31. Predict:
console.log(null && "hello");
console.log(undefined && "hello");
console.log(0 && "hello");
console.log("" && "hello");
console.log(1 && 0);
Q32. Write a function getDisplayName(user) that returns user.nickname, falling back to user.name, falling back to "Anonymous". Use ??.
1.18.d — Assignment Operators (Q33–Q40)
Q33. What is the value of score after:
let score = 100;
score += 50;
score -= 30;
score *= 2;
score /= 4;
score %= 7;
Q34. Predict the output:
let x = 0;
x ||= 10;
console.log(x);
let y = 0;
y ??= 10;
console.log(y);
Q35. What is the difference between x ||= 5 and x ??= 5?
Q36. What does x &&= x.trim() do? When does the assignment NOT happen?
Q37. Debug: Why does this create a global variable?
let x = y = 10;
Q38. Predict:
let str = "Hello";
str += " ";
str += "World";
console.log(str);
Q39. Write code that initializes config.retries to 3 only if it is currently null or undefined, using a logical assignment operator. It should NOT overwrite 0.
Q40. Predict:
const obj = { a: null, b: 0, c: "", d: false };
obj.a ??= "default";
obj.b ??= "default";
obj.c ??= "default";
obj.d ??= "default";
console.log(obj);
1.18.e — typeof Operator (Q41–Q48)
Q41. What does typeof null return? Why?
Q42. Predict the typeof result for each:
typeof 42
typeof "hello"
typeof true
typeof undefined
typeof null
typeof []
typeof {}
typeof function(){}
typeof NaN
typeof Symbol()
typeof 10n
Q43. Why does typeof on an undeclared variable NOT throw an error? Give a practical use case.
Q44. How would you check if a value is an array? Why does typeof fail here?
Q45. What is the difference between typeof x === "function" and x instanceof Function?
Q46. Write a function getType(value) that returns the accurate type name, including "null" for null and "array" for arrays.
Q47. Predict:
console.log(typeof class Foo {});
console.log(typeof Math);
console.log(typeof JSON);
console.log(typeof console);
Q48. Debug: This validation function has a bug. Identify and fix it:
function isValidNumber(val) {
return typeof val === "number";
}
console.log(isValidNumber(42)); // true (correct)
console.log(isValidNumber(NaN)); // ? (should be false)
1.18.f — Truthy and Falsy Values (Q49–Q56)
Q49. List all 8 falsy values in JavaScript.
Q50. Are these truthy or falsy?
[]
{}
"0"
"false"
" "
new Boolean(false)
-1
Infinity
Q51. Explain why [] == false is true even though [] is truthy.
Q52. Debug: This function has a bug when score is 0:
function displayScore(score) {
if (!score) {
return "No score yet";
}
return `Score: ${score}`;
}
Fix it so that displayScore(0) returns "Score: 0".
Q53. What is the difference between !!value and Boolean(value)?
Q54. Why is if (arr) NOT a reliable way to check if an array has elements?
Q55. Predict:
console.log(Boolean([]));
console.log(Boolean(""));
console.log(Boolean("0"));
console.log(Boolean(0));
console.log(Boolean(new Number(0)));
Q56. Write code that checks if user.email exists and is a non-empty string.
1.18.g — Type Coercion (Q57–Q67)
Q57. Predict:
console.log("5" + 3);
console.log("5" - 3);
console.log("5" * "2");
console.log(true + true);
console.log(null + 1);
console.log(undefined + 1);
Q58. What is the result of 1 + 2 + "3" + 4? Walk through each step.
Q59. What is the difference between Number("42px") and parseInt("42px", 10)?
Q60. Predict:
console.log(Number(""));
console.log(Number(" "));
console.log(Number(null));
console.log(Number(undefined));
console.log(Number([]));
console.log(Number([5]));
console.log(Number([1, 2]));
console.log(Number({}));
Q61. What is the result of [] + []? What about [] + {}?
Q62. Why does {} + [] evaluate to 0 in the browser console but "[object Object]" when wrapped in console.log()?
Q63. What does the unary + operator do? How is +"42" different from parseInt("42")?
Q64. Predict:
const obj = {};
obj[1] = "one";
obj[true] = "yes";
console.log(obj["1"]);
console.log(obj["true"]);
console.log(Object.keys(obj));
Q65. Debug: This function sometimes returns NaN. Why?
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // 8
console.log(add("5", 3)); // ?
console.log(add(undefined, 3)); // ?
Q66. Write a function that safely converts a string to a number, returning null if the string is not a valid number (including empty strings).
Q67. Predict:
console.log([] - []);
console.log({} - {});
console.log([] == 0);
console.log("" == 0);
console.log("" == false);
console.log("0" == false);
console.log("" == "0");
Answer hints
| Q | Hint |
|---|---|
| Q2 | Post-increment returns old value; pre-increment returns new |
| Q3 | ** is right-associative: 2 ** (3 ** 2) = 2 ** 9 = 512 |
| Q10 | * before +; ** before * |
| Q15 | > and >= use numeric coercion; == has special null rules |
| Q16 | Both strings → lexicographic; string vs number → numeric |
| Q20 | Use === to fix: return val === 0 |
| Q25 | ` |
| Q26 | Cannot mix ?? with ` |
| Q34 | ` |
| Q40 | ??= only assigns for null/undefined; 0, "", false are kept |
| Q41 | Historical bug — null's type tag was 0 (same as objects) |
| Q48 | typeof NaN === "number" — add !Number.isNaN(val) check |
| Q51 | [] → "" → 0, false → 0, 0 == 0 |
| Q52 | Use if (score == null) or if (score === null || score === undefined) |
| Q58 | 1+2=3, 3+"3"="33", "33"+4="334" |
| Q61 | [] → "" + "" = ""; [] → "" + "[object Object]" |
| Q67 | 0, NaN, true, true, true, true, false |