Fill: instanceof Guard
Fill in the Blank Easy
Question
Fill in the blank to narrow value to a Date:
Code
function formatDate(value: Date | string): string {
if (value ___ Date) {
return value.toISOString();
}
return value;
}
Hint
instanceof checks if an object is an instance of a class.
Explanation
The operator is instanceof. value instanceof Date checks if value is a Date instance at runtime. Inside the if block, TypeScript narrows value to Date, allowing access to Date methods.
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.