# Type Conversion & Coercion — JavaScript

Source: https://www.geekswithgeeks.com/en/javascript/js-type-conversion

> Explicit conversion vs JS's automatic coercion.

## Explicit conversion

Convert on purpose with `Number()`, `String()`, `Boolean()`.

```js
Number("42");     // 42
String(42);       // "42"
Boolean(0);       // false
Boolean("");      // false
Boolean("0");     // true (non-empty string!)
```

## Implicit coercion

JS auto-converts types in operations — `+` with a string triggers concatenation, but `-`, `*`, `/` coerce operands to numbers.

## The classic gotchas

Coercion produces some famously surprising results — know them so they don't surprise you in real code.

```js
console.log("5" + 3);     // "53"
console.log("5" - 3);     // 2
console.log(1 + true);    // 2
console.log([] + []);     // ""
```

## == vs ===

`==` coerces types before comparing; `===` compares value **and** type. Prefer `===` almost always.
