TypeScript Null and Undefined
Learn how TypeScript handles null and undefined, why they cause bugs in JavaScript, and how strictNullChecks keeps your code safe.
David Miller
January 15, 2026
0.1k2
In JavaScript, many bugs happen because values are missing.
TypeScript helps you **handle missing values safely**.
## What are null and undefined?
- undefined: value not assigned
- null: intentionally empty
```ts
let a: number;
console.log(a); // undefined
let b: number | null = null;
```
## The problem in JavaScript
```ts
let name = null;
console.log(name.length); // crash at runtime
```
## TypeScript safety
```ts
let name: string | null = null;
// name.length ❌ error
if (name) {
console.log(name.length); // safe
}
```
## strictNullChecks
When enabled:
- null/undefined must be handled explicitly
- prevents runtime crashes
## Optional chaining
```ts
type User = { profile?: { email: string } };
const user: User = {};
console.log(user.profile?.email); // undefined, safe
```
## Graph
```mermaid
flowchart LR
A[Value] --> B{null/undefined?}
B -->|Yes| C[Handle safely]
B -->|No| D[Use value]
```
## Remember
- Always handle null/undefined
- Use strictNullChecks
- Optional chaining avoids crashes
#TypeScript#Beginner#Null Safety