Debug fifteen JavaScript programs about data types: invalid literals, TypeErrors from null, undefined, symbols and BigInt, and silent bugs from floating-point comparison, isNaN, typeof and shared references.
Question 11Hard
The code below stops with an uncaught error. Which statement correctly describes the problem?
main.js
const order = { id: 12345678901234567890n, total: 499 };
console.log(JSON.stringify(order));
ATypeError: JSON.stringify does not know how to serialize a BigInt
BIt prints {"id":12345678901234567890,"total":499}
CIt prints {"id":"12345678901234567890n","total":499}
DIt prints {"total":499}
Correct Answer: Option A
JSON has no BigInt type. Convert it yourself, e.g. JSON.stringify(order, (k, v) => typeof v === "bigint" ? v.toString() : v).
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
main.js
function describe(v) {
if (typeof v === "array") return "list";
if (typeof v === "object") return "record";
return typeof v;
}
console.log(describe([1, 2]), describe(null), describe({}));
Adescribe must be an arrow function
BObjects cannot be passed to functions
CThe second if should use ==
Dtypeof never returns "array", and typeof null is "object" – use Array.isArray(v) and v === null first
Correct Answer: Option D
Check null and arrays before the generic object test: if (v === null) return "null"; if (Array.isArray(v)) return "list";
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·