Type Conversions
MediumIn plain terms
JavaScript converts types automatically (coercion) or you can convert explicitly with Number(), String(), Boolean(). Understand truthy/falsy values to avoid bugs.
What you need to know
- •Explicit: Number(), String(), Boolean()
- •Implicit coercion in expressions
- •Falsy values
Try it yourself
Copy the code below and run it in your browser console or a code editor:
// Explicit conversion
String(123); // "123"
Number('42'); // 42
Boolean(1); // true
// Implicit coercion
'5' + 3; // "53" (string concatenation)
'5' - 3; // 2 (numeric subtraction)
// Falsy: false, 0, '', null, undefined, NaN
Boolean(''); // false
Boolean(0); // falseLearn more
Dive deeper with these trusted resources: