100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

Type Conversion and Coercion in JavaScript

Learn the difference between explicit type conversion and implicit coercion, and the common pitfalls each creates.

Basics & Data TypesIntermediate9 min readJul 8, 2026
Analogies

1. Introduction

JavaScript automatically converts values between types in many situations, a behavior called type coercion. Developers can also convert types explicitly using functions such as Number(), String(), and Boolean(). Understanding both is essential because coercion happens silently and can produce surprising results if you don't anticipate it.

🏏

Cricket analogy: JavaScript's automatic coercion is like an umpire silently rounding a marginal no-ball call without announcing it, while explicit conversion is the third umpire deliberately reviewing the replay with Number(), String(), or Boolean()-style tools before making the call clear.

2. Syntax

javascript
// Explicit conversion
Number("42");     // 42
String(42);        // "42"
Boolean(1);         // true

// Implicit coercion
"5" + 3;            // "53" (string concatenation)
"5" - 3;             // 2 (numeric subtraction)
if ("non-empty") {} // truthy check coerces to boolean

3. Explanation

Explicit conversion uses wrapper functions to convert a value on purpose: Number() converts to a number, String() converts to a string, and Boolean() converts to a boolean using JavaScript's truthy/falsy rules. Implicit coercion happens automatically when operators expect a certain type — the + operator prefers string concatenation if either operand is a string, while -, *, and / always coerce operands to numbers.

🏏

Cricket analogy: Explicit Boolean() conversion decides if a 'did he score' flag is truthy or falsy, just like a scorer explicitly marking out or not out; implicitly, adding a player name string to a run count like 'Kohli' + 50 concatenates into text, while subtracting always forces numbers, like calculating strike rate.

Falsy values in JavaScript are: false, 0, -0, 0n, "", null, undefined, and NaN. Every other value, including '0' (a non-empty string) and empty objects/arrays, is truthy.

🏏

Cricket analogy: Falsy values are like a scoreboard treating a duck (0 runs), a blank entry, or a no-result match as nothing happened, while a jersey number string like '0' or an empty scorecard object still counts as a real, truthy entry on the sheet.

Gotcha: '5' + 3 produces the string '53' because + triggers string concatenation when either side is a string, but '5' - 3 produces the number 2 because - always coerces both sides to numbers. Also, NaN !== NaN (NaN is never equal to anything, even itself — use Number.isNaN() to test for it), and [] == false is true because [] is coerced to '' then to 0, matching false's coercion to 0.

4. Example

javascript
console.log('5' + 3);
console.log(5 + 3);
console.log('5' - 3);
console.log('5' * '2');
console.log(NaN === NaN);
console.log([] == false);
console.log(Number('  42  '));
console.log(Number('abc'));
console.log(String(null));
console.log(Boolean(''));
console.log(Boolean('0'));

5. Output

text
53
8
2
10
false
true
42
NaN
null
false
true

6. Key Takeaways

  • Explicit conversion (Number(), String(), Boolean()) makes intent clear; implicit coercion happens automatically via operators.
  • + prefers string concatenation if either operand is a string; -, *, /, and % always coerce to numbers.
  • Falsy values are false, 0, -0, 0n, '', null, undefined, and NaN — everything else is truthy.
  • NaN is never equal to anything, including itself; use Number.isNaN() to check for it.
  • == performs type coercion before comparing, while === compares both type and value without coercion; prefer === to avoid surprises.

Practice what you learned

Was this page helpful?

Topics covered

#JavaScript#JavaScriptProgrammingStudyNotes#Programming#TypeConversionAndCoercionInJavaScript#Type#Conversion#Coercion#Syntax#StudyNotes#SkillVeris