Null and undefined represent missing or empty values in TypeScript. They help us handle cases where a value might not exist yet.
Null and undefined types in Typescript
Introduction
Syntax
Typescript
let value: null; let value2: undefined; let maybeNumber: number | null | undefined;
null means a variable has no value.
undefined means a variable has not been assigned a value yet.
Examples
null.Typescript
let emptyValue: null = null;
undefined.Typescript
let notAssigned: undefined = undefined;
null, or undefined.Typescript
let optionalNumber: number | null | undefined = null; optionalNumber = 5; optionalNumber = undefined;
Sample Program
This program shows how to check if a value is null, undefined, or a string, then prints the length if it is a string.
Typescript
function printLength(text: string | null | undefined) { if (text === null) { console.log('Text is null'); } else if (text === undefined) { console.log('Text is undefined'); } else { console.log(`Text length is ${text.length}`); } } printLength(null); printLength(undefined); printLength('hello');
Important Notes
Use strict equality === to check for null or undefined.
By default, null and undefined are different types in TypeScript.
You can allow variables to hold these types by using union types like string | null | undefined.
Summary
Null means a variable has no value.
Undefined means a variable has not been assigned a value yet.
Use union types to allow variables to hold null or undefined.
Practice
1. What does
null represent in TypeScript?easy
Solution
Step 1: Understand the meaning of
nullnullmeans a variable explicitly has no value.Step 2: Compare with other options
The incorrect options describe different states or values, notnull.Final Answer:
A variable that has no value assigned -> Option DQuick Check:
nullmeans no value [OK]
Hint: Remember: null means no value assigned [OK]
Common Mistakes:
- Confusing null with undefined
- Thinking null means variable is undeclared
- Assuming null means zero or empty string
2. Which of the following is the correct way to declare a variable that can hold a string or
null in TypeScript?easy
Solution
Step 1: Recall union type syntax
In TypeScript, the pipe|is used to allow multiple types.Step 2: Check each option
let name: string | null; uses|correctly. The other options use invalid syntax.Final Answer:
let name: string | null; -> Option AQuick Check:
Union types use | symbol [OK]
Hint: Use | to combine types like string | null [OK]
Common Mistakes:
- Using & instead of | for union types
- Writing 'or' instead of |
- Using invalid syntax like ? null
3. What will be the output of this TypeScript code?
let value: number | undefined; console.log(value);
medium
Solution
Step 1: Understand variable initialization
The variablevalueis declared but not assigned, so it defaults toundefined.Step 2: Check console.log output
Loggingvalueprintsundefinedbecause it has no assigned value.Final Answer:
undefined -> Option AQuick Check:
Unassigned variable = undefined [OK]
Hint: Uninitialized variables print undefined [OK]
Common Mistakes:
- Expecting null instead of undefined
- Assuming default is zero
- Thinking it causes an error
4. Identify the error in this TypeScript code:
let age: number; age = null;
medium
Solution
Step 1: Check variable type and assignment
ageis declared asnumberonly, so it cannot holdnull.Step 2: Understand TypeScript strict null checks
By default,nullis not assignable tonumberunless union type includesnull.Final Answer:
Cannot assign null to a variable of type number -> Option CQuick Check:
Assigning null to number causes error [OK]
Hint: Add | null to allow null assignment [OK]
Common Mistakes:
- Ignoring TypeScript strict null checks
- Thinking null is allowed by default
- Confusing missing semicolon as error
5. How can you declare a function parameter in TypeScript that accepts either a string,
null, or undefined?hard
Solution
Step 1: Understand union types for parameters
To accept multiple types, use|to combine them.Step 2: Analyze each option
function greet(name: string | null | undefined) { console.log(name); } correctly usesstring | null | undefined. function greet(name: string & null & undefined) { console.log(name); } uses&which means intersection (not correct). function greet(name?: string & null) { console.log(name); } uses&which means intersection (not correct). function greet(name: string | null?) { console.log(name); } has invalid syntax.Final Answer:
function greet(name: string | null | undefined) { console.log(name); } -> Option BQuick Check:
Use | to allow multiple types in parameters [OK]
Hint: Use | to combine string, null, and undefined types [OK]
Common Mistakes:
- Using & instead of | for union types
- Confusing optional parameter with undefined union
- Writing invalid syntax like null?
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
