What are the falsy values in TypeScript and JavaScript?
Tags: typescript
Published: 2021-11-12 00:00:00

---
  title: What are the falsy values in TypeScript and JavaScript?
  published: true
  tags: TypeScript
  date: Fri Nov 12 2021 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/falsy-values-in-typescript-javascript
  cover_image: https://melvingeorge.me/_next/static/images/main-590e9700e170e0592bc07ec03a7d890d.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/falsy-values-in-typescript-javascript)
  
Falsy values are those values that get coerced to the boolean type `false` in TypeScript and JavaScript.

These are the following `falsy` values in TypeScript and JavaScript:

- **`0`**
- **`false`**
- **`NaN`**
- **`"" (empty string)`**
- **`0n (bigint version of zero)`**
- **`null`**
- **`undefined`**

All the above values when used with an `if` conditional gets coerced to the boolean type `false`.

To understand it correctly, let's use an `if` conditional statement check and use all the above `falsy` values.

### `0` value example

```ts
// using the "0" value in an if conditional statement
if (0) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
```

- In the above code since the `0` value is a `falsy` value, the else block will be executed and the output of `else block is executed` is shown to the console

### `false` value example

```ts
// using the "false" value in an if conditional statement
if (false) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
```

- In the above code since the `false` value is a `falsy` value, the else block will be executed and the output of `else block is executed` is shown to the console

### `NaN` value example

```ts
// using the "NaN" value in an if conditional statement
if (NaN) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
```

- In the above code since the `NaN` value is a `falsy` value, the else block will be executed and the output of `else block is executed` is shown to the console

### `""` (empty string) example

```ts
// using the empty string value in an if conditional statement
if ("") {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
```

- In the above code since the `""` value is a `falsy` value, the else block will be executed and the output of `else block is executed` is shown to the console

### `0n` value example

```ts
// using the `0n` value in an if conditional statement
if (0n) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
```

- In the above code since the `0n` value is a `falsy` value, the else block will be executed and the output of `else block is executed` is shown to the console

### `null` value example

```ts
// using the `null` value in an if conditional statement
if (null) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
```

- In the above code since the `null` value is a `falsy` value, the else block will be executed and the output of `else block is executed` is shown to the console

### `undefined` value example

```ts
// using the `undefined` value in an if conditional statement
if (undefined) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
```

- In the above code since the `undefined` is a `falsy` value, the else block will be executed and the output of `else block is executed` is shown to the console

See the above codes live in [codesandbox](https://codesandbox.io/s/falsy-values-in-typescript-and-javascript-v8ywo?file=/src/index.ts).

That's all 😃!

### Feel free to share if you found this useful 😃.

---