JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Type Coercion

javascript
junior
type-coercion

How does type coercion work in JavaScript?

type-coercion
operators
truthy
falsy
fundamentals
Quick Answer

Type coercion is JavaScript's automatic conversion of values from one type to another. It happens implicitly with operators like +, ==, and if(), or explicitly using Number(), String(), and Boolean().

Detailed Explanation

JavaScript is a dynamically typed language that automatically converts types when needed.

Implicit coercion (automatic):

  • String coercion: + with a string converts the other operand to string
  • Numeric coercion: -, *, /, >, < convert to numbers
  • Boolean coercion: if(), &&, ||, ! convert to boolean

Explicit coercion (intentional):

  • Number(value), parseInt(), parseFloat()
  • String(value), .toString()
  • Boolean(value), !!value

Falsy values (convert to false): false, 0, -0, 0n, "", null, undefined, NaN

Everything else is truthy, including [], {}, and "0".

Code Examples

Implicit type coercionJavaScript
// String coercion with +
'5' + 3       // '53' (number → string)
'5' + true    // '5true'
'5' + null    // '5null'

// Numeric coercion with -, *, /
'5' - 3       // 2 (string → number)
'5' * '2'     // 10
true + true   // 2 (boolean → number)

// Boolean coercion
if ('hello') {} // true (non-empty string)
if (0) {}       // false
if ([]) {}      // true (empty array is truthy!)
if ({}) {}      // true (empty object is truthy!)

// Common gotchas
[] + []         // '' (empty string)
[] + {}         // '[object Object]'
{} + []         // 0 (in console; as expression: '[object Object]')
null + 1        // 1
undefined + 1   // NaN

Real-World Applications

Use Cases

Form Input Parsing

Converting string inputs to numbers for calculations using explicit coercion like Number(input.value)

Default Values

Using truthy/falsy checks like value || 'default' for setting fallback values in function parameters

API Data Handling

Ensuring correct types when processing JSON responses where numbers may arrive as strings

Mini Projects

Type Coercion Quiz

beginner

Build an interactive game testing knowledge of coercion results for various JavaScript expressions

Coercion Table Generator

intermediate

Build a tool that generates a complete equality and coercion table for all JavaScript type combinations

Industry Examples

ESLint

The no-implicit-coercion rule flags shorthand conversions like !!foo and +foo, suggesting explicit Boolean() and Number() instead

TypeScript

Strict mode (strictNullChecks, noImplicitAny) catches type-safety issues at compile time that would cause unexpected coercion at runtime

Resources

MDN - Type coercion

docs

JavaScript.info - Type Conversions

article

Related Questions

What is the difference between == and === in JavaScript?

junior
operators

What are the primitive data types in JavaScript?

junior
data-types
Previous
What is event delegation in JavaScript and why is it useful?
Next
What is the ternary operator and how does it differ from an if-else statement?
PrevNext