JavaScript Type Conversion

Implicit Conversion

JavaScript automatically converts values when needed:

Loading...
Output:

Explicit Conversion

Loading...
Output:

Useful when you want predictable behavior in code.

parseInt() and parseFloat()

These are used for converting strings to numbers:

parseInt("42px")      // 42
parseFloat("3.14abc")  // 3.14
parseInt("abc")        // NaN

NaN and isNaN()

NaN stands for "Not a Number":

let x = Number("hello");
console.log(x);          // NaN
console.log(isNaN(x));   // true

Falsy vs Truthy

  • Falsy: 0, "", null, undefined, NaN, false
  • Truthy: All other values
Boolean(0)        // false
Boolean("text")   // true

Best Practices

  • Use String(), Number(), Boolean() for clarity
  • Prefer parseInt() with a radix (e.g., parseInt("10", 10))
  • Use === for strict comparisons to avoid coercion issues

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic