Predict Union Narrowing Output
Predict Output Easy
Question
What does this code print?
Code
function process(value: string | number) {
if (typeof value === "string") {
console.log(value.toUpperCase());
} else {
console.log(value * 2);
}
}
process("hi");
process(5);
Hint
The first call passes a string, the second passes a number.
Explanation
When process("hi") is called, typeof value is "string", so it logs "hi".toUpperCase() = "HI". When process(5) is called, typeof value is "number", so it logs 5 * 2 = 10. The output is HI on one line and 10 on the next.
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.