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
// 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 boolean3. 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
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
53
8
2
10
false
true
42
NaN
null
false
true6. 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
1. What is the result of '5' + 3 in JavaScript?
2. What is the result of '5' - 3 in JavaScript?
3. What does NaN === NaN evaluate to?
4. What does [] == false evaluate to and why?
5. Which value(s) are falsy in JavaScript?
Was this page helpful?
You May Also Like
Data Types in JavaScript
Understand JavaScript's primitive and reference data types and how the typeof operator identifies them.
Operators in JavaScript
Survey JavaScript's arithmetic, comparison, logical, and ternary operators and how they behave with type coercion.
Variables in JavaScript (var, let, const)
Learn how var, let, and const differ in scope, hoisting, and reassignment when declaring variables in JavaScript.
Optional Chaining and Nullish Coalescing
The ES2020 ?. and ?? operators for safely accessing nested properties and providing defaults only for null/undefined.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics