# Nullish Values & !

> Handling nothingness and the danger of the non-null assertion.

Subject: TypeScript | Lesson 8 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/nullish-values
Previous: [Enums vs Const Objects](https://codewithanimation.in/typescript/enums-vs-const-objects.md) | Next: [Literal Types](https://codewithanimation.in/typescript/literal-types.md)

## Strict Null Checks

Historically, JavaScript has two ways to represent 'nothing': `null` (intentional absence) and `undefined` (uninitialized).

By default, TypeScript forces you to explicitly handle these values if your `tsconfig.json` has `strictNullChecks: true` (which all modern projects should). If a variable can be a string or null, you cannot call string methods on it until you prove it is not null.

## The Billion Dollar Mistake

Tony Hoare famously called the invention of the null reference his 'billion-dollar mistake' because it caused decades of unhandled runtime crashes.

TypeScript prevents this by forcing you to write `if (myVar)` before accessing properties on potentially null variables.

## The ! Operator

Sometimes, you know a value isn't null, but TypeScript doesn't. For example, querying the DOM for an element you know exists.

You can append `!` to a variable (the Non-Null Assertion Operator) to tell the compiler 'Trust me, this isn't null'.

```typescript
// returns HTMLElement | null
const el = document.getElementById("app");

// Error: Object is possibly 'null'.
el.innerHTML = "hi";

// We silence the compiler with '!'
el!.innerHTML = "hi";
```

## Common mistake: The danger of ! overconfidence

The `!` operator does not change your code at runtime. It is entirely erased when compiled.

If you use `!` and you are wrong (e.g. the element was deleted from the DOM), your app will crash at runtime with `Cannot read properties of null`. You have successfully lied to the compiler and brought back the billion dollar mistake.

## Tip: Alternatives to !

Instead of using `!`, prefer safer alternatives like Optional Chaining (`el?.innerHTML = "hi"`) which safely fails silently at runtime, or write a proper type guard (`if (!el) throw new Error(...)`).

## Try it: Try it

Watch how appending `!` instantly removes a null-check compiler error, but leaves the runtime completely unprotected.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/nullish-values

## Check yourself

### Question 1

What does the non-null assertion operator (!) actually do at runtime?

- A. It throws an error if the value is null.
- B. It converts null values to undefined.
- C. Absolutely nothing. It is removed during compilation.

Correct answer: C

The '!' operator is purely for the compiler. If the value actually is null at runtime, your app will still crash.

- Why not A: It generates zero runtime code.
- Why not B: It does not modify values.

## Remember this

- `strictNullChecks` forces you to handle `null` and `undefined`.
- The `!` operator tells the compiler to ignore potential nulls.
- Using `!` is dangerous because it provides zero runtime protection.
- Prefer Optional Chaining (`?.`) or explicit `if` checks.
