DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. JavaScript Coercion: How Type Conversion Actually Works
XLinkedInReddit
MediumFrontend Engineering

JavaScript Coercion: How Type Conversion Actually Works

D
DevPrep Team
February 10, 2026·2 min read·0
Table of Contents
  • Explicit vs Implicit Coercion
  • ToNumber Rules
  • ToString Rules
  • ToBoolean Rules (Falsy Values)
  • The + Operator
  • == vs === Deep Dive
  • Best Practices

Type coercion is one of JavaScript's most misunderstood features. Understanding the rules eliminates "wat" moments.

Explicit vs Implicit Coercion

// Explicit (you do it intentionally)
Number("42");     // 42
String(42);       // "42"
Boolean(0);       // false

// Implicit (JavaScript does it for you)
"5" * 2;          // 10 (string → number)
5 + "";           // "5" (number → string)
if ("hello") {}   // true (string → boolean)

ToNumber Rules

Number(undefined)  // NaN
Number(null)       // 0 (!)
Number(true)       // 1
Number(false)      // 0
Number("")         // 0 (!)
Number(" ")        // 0 (!)
Number("42")       // 42
Number("42px")     // NaN
Number([])         // 0 ([] → "" → 0)
Number([1])        // 1 ([1] → "1" → 1)
Number([1, 2])     // NaN ([1,2] → "1,2" → NaN)
Number({})         // NaN

ToString Rules

String(null)       // "null"
String(undefined)  // "undefined"
String(true)       // "true"
String([1, 2, 3])  // "1,2,3"
String({})         // "[object Object]"
String([])         // "" (empty string!)

ToBoolean Rules (Falsy Values)

// Only these are falsy:
Boolean(false)     // false
Boolean(0)         // false
Boolean(-0)        // false
Boolean(0n)        // false (BigInt zero)
Boolean("")        // false
Boolean(null)      // false
Boolean(undefined) // false
Boolean(NaN)       // false

// EVERYTHING else is truthy:
Boolean("0")       // true (!)
Boolean([])        // true (!)
Boolean({})        // true (!)
Boolean("false")   // true (!)

The + Operator

// If either operand is a string → string concatenation
"5" + 3;           // "53" (number → string)
3 + "5";           // "35"
"" + 42;           // "42" (quick number → string conversion)

// Otherwise → numeric addition
5 + 3;             // 8
5 + null;          // 5 (null → 0)
5 + true;          // 6 (true → 1)
5 + undefined;     // NaN (undefined → NaN)

== vs === Deep Dive

// == performs type coercion
"5" == 5;          // true (string → number)
null == undefined; // true (special rule!)
null == 0;         // false (null only == null or undefined)
"" == false;       // true ("" → 0, false → 0)
[] == false;       // true ([] → "" → 0, false → 0)

// === no coercion
"5" === 5;         // false
null === undefined; // false

Best Practices

  • Always use === except when intentionally checking null/undefined (x == null)
  • Be explicit: use Number(), String(), Boolean() instead of implicit coercion
  • Know the falsy values — they come up in interviews constantly

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Explicit vs Implicit Coercion
  • ToNumber Rules
  • ToString Rules
  • ToBoolean Rules (Falsy Values)
  • The + Operator
  • == vs === Deep Dive
  • Best Practices

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.