Predict typeof Guard Output
Predict Output Easy
Question
What does this code print?
Code
function describe(x: string | number | boolean) {
if (typeof x === "string") return "text:" + x;
if (typeof x === "number") return "num:" + x;
return "bool:" + x;
}
console.log(describe("hi"));
console.log(describe(42));
console.log(describe(true));
Hint
Each typeof check narrows the type and returns a different prefix.
Explanation
describe("hi") — typeof is "string", returns "text:hi". describe(42) — typeof is "number", returns "num:42". describe(true) — typeof is "boolean", returns "bool:true". Output: text:hi, num:42, bool:true on separate lines.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.