# Type Conversion (Explicit)

> How to intentionally convert values between strings, numbers, and booleans using JavaScript's built-in casting functions.

Subject: JavaScript | Lesson 7 of 63 | 5 min read
Interactive lesson: https://codewithanimation.in/javascript/type-conversion
Previous: [Numbers, BigInt & Math](https://codewithanimation.in/javascript/numbers-and-math.md) | Next: [null, undefined & NaN](https://codewithanimation.in/javascript/null-undefined-nan.md)

## What is explicit type conversion?

While JavaScript often coerces types automatically (implicit coercion), it is much safer to convert them intentionally (explicit conversion).

JavaScript provides three primary functions for this: `String()`, `Number()`, and `Boolean()`.

## Why cast explicitly?

Implicit coercion can lead to very unpredictable bugs, like `5 + 3` evaluating to `53`. By explicitly converting the string to a number first using `Number('5') + 3`, the code evaluates predictably to `8`.

Explicit casting makes your intent obvious to other developers reading your code.

## How casting behaves

`String(value)`: Converts almost anything to a sensible string. `String(null)` becomes `'null'`, and `String([1, 2])` becomes `'1,2'`.

`Number(value)`: Very strict. If a string contains any letters, it evaluates to `NaN` (Not-a-Number). `Number(true)` becomes `1`, and `Number(false)` becomes `0`.

`Boolean(value)`: Converts based on 'truthiness'. The six falsy values (`0`, `''`, `null`, `undefined`, `NaN`, `false`) become `false`. Everything else, including empty arrays `[]` and empty objects `{}`, becomes `true`.

## Common mistake: parseInt() vs Number()

`parseInt('100px')` will return `100` because it parses until it hits a non-number. `Number('100px')` returns `NaN` because the entire string is not a valid number. Be careful which one you choose!

## Tip: Shorthand casting

You might see developers use `+val` to cast to a number, or `!!val` to cast to a boolean. While clever, this is less readable than `Number(val)` or `Boolean(val)` for beginners.

## Try it: Casting Simulator

See how different JavaScript values are handled when forced through the three main type conversion functions.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/type-conversion

## Check yourself

### Question 1

What does `Boolean([])` evaluate to?

- A. true
- B. false
- C. undefined

Correct answer: A

In JavaScript, all objects (including arrays, even empty ones) are truthy. Therefore, casting `[]` to a Boolean results in `true`.

- Why not B: Incorrect. While an empty string is falsy, an empty array is an object, which is truthy.
- Why not C: Incorrect. Boolean() always returns exactly true or false.

### Question 2

What is the result of `Number("42px")`?

- A. NaN
- B. 42
- C. "42"

Correct answer: A

The `Number()` function is strict. If the entire string cannot be parsed as a valid number, it bails out and returns `NaN` (Not-a-Number).

- Why not B: Incorrect. That would be the result of `parseInt('42px')`.
- Why not C: Incorrect. The Number() function always attempts to return a numeric type (or NaN).

### Question 3

Which function safely converts `null` to the string `"null"`?

- A. String(null)
- B. null.toString()
- C. Number(null)

Correct answer: A

`String()` is a global function that safely handles `null` and `undefined`. Attempting to call `.toString()` on either of those values will crash your program with a TypeError.

- Why not B: Incorrect. `null` is a primitive without a prototype, so calling a method on it throws a TypeError.
- Why not C: Incorrect. `Number(null)` evaluates to 0.

## Remember this

- Use explicit conversion (`Number()`, `String()`, `Boolean()`) to make your code predictable.
- `Number()` is strict and returns `NaN` if the string contains non-numeric characters.
- `Boolean()` evaluates based on truthiness. Empty arrays and objects are truthy.
- `parseInt()` extracts numbers from strings, whereas `Number()` validates the whole string.
