# Operators & Type Coercion

> How JavaScript compares, computes, and quietly converts values behind the scenes when types don't match.

Subject: JavaScript | Lesson 4 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/operators-and-coercion
Previous: [Data Types & Primitives](https://codewithanimation.in/javascript/data-types-and-primitives.md) | Next: [Strings & Template Literals](https://codewithanimation.in/javascript/strings-and-template-literals.md)

## What are operators and coercion?

Operators are symbols used to perform operations on operands. JavaScript includes arithmetic (`+`, `-`, `*`, `/`), logical (`&&`, `||`, `!`), and comparison operators (`==`, `===`, `>`, `<`).

Type coercion is JavaScript's automatic or implicit conversion of values from one data type to another (such as strings to numbers) when performing an operation.

## Why does it matter?

JavaScript is a dynamically typed language, which means it tries to be helpful by converting types automatically to make an operation succeed.

While this seems convenient, implicit coercion is the source of many notoriously confusing bugs. Understanding how values convert is essential to writing predictable code.

## Strict vs Loose Equality

The loose equality operator (`==`) compares two values after attempting to convert them to a common type. For example, `5 == '5'` is true because the string is coerced to a number.

The strict equality operator (`===`) compares both the value and the type without doing any coercion. `5 === '5'` is false.

Always use `===` unless you have a specific reason to use loose equality.

## Common mistake: Falsy values and the || operator

JavaScript has exactly six 'falsy' values: `false`, `0`, `""` (empty string), `null`, `undefined`, and `NaN`. Everything else is 'truthy'.

Using the logical OR operator (`||`) to provide a default value (e.g. `const limit = config.limit || 10`) will fail if `config.limit` is legally `0`. Use the nullish coalescing operator (`??`) to only fall back on `null` or `undefined`.

## Tip: Explicit conversion is safer

Instead of relying on implicit coercion, convert types explicitly. Use `Number(val)` instead of `+val`, and `String(val)` instead of `val + ''`. Your code will be much easier to read and debug.

## Try it: Coercion Comparator

Compare different values using both loose (`==`) and strict (`===`) equality. See how JavaScript coerces the types before comparing them loosely.

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

## Check yourself

### Question 1

What is the result of `"5" + 3`?

- A. "53"
- B. 8
- C. NaN

Correct answer: A

When using the `+` operator, if one operand is a string, JavaScript converts the other to a string and concatenates them.

- Why not B: Incorrect. The `+` operator favors string concatenation if any operand is a string.
- Why not C: Incorrect. JavaScript coerces the number 3 to a string, not the other way around.

### Question 2

What is the result of `"5" - 3`?

- A. 2
- B. "53"
- C. "2"

Correct answer: A

Unlike `+` which can mean concatenation, `-` only means subtraction. JavaScript coerces the string '5' into the number 5, resulting in 2.

- Why not B: Incorrect. The `-` operator is only for math, so it coerces strings to numbers.
- Why not C: Incorrect. The result of subtraction is always a number.

### Question 3

Which of these values is NOT falsy?

- A. "false" (a string containing the word false)
- B. 0
- C. "" (an empty string)

Correct answer: A

Any non-empty string is truthy, even if it contains the word 'false' or a space character ' '.

- Why not B: Incorrect. The number 0 is a falsy value.
- Why not C: Incorrect. An empty string evaluates to false.

## Remember this

- Always use strict equality (`===`) instead of loose equality (`==`) to avoid unexpected coercion.
- The 6 falsy values are: false, 0, "", null, undefined, NaN.
- The `+` operator coerces to string if any operand is a string. Other math operators (`-`, `*`, `/`) coerce to numbers.
