Skip to main content
Module 2 · Lesson 4 of 48/32 in the course~15 min
Module lessons (4/4)

Type conversions

Lesson preview5 hands-on exercises readyJavaScript Course · Module 2 — Strings and numbers

Number, String, parseInt, parseFloat, toFixed and when NaN bites you.

What you'll learn in this lesson

  • Choose correctly between Number(str), parseInt(str, 10) and parseFloat(str).
  • Know the Boolean conversion rules (Boolean(x)) and the critical falsehoods ('', 0).
  • Recognize the cases in which NaN arises and distinguish it from finite values with Number.isNaN and Number.isFinite.
  • Write defensive parsers that validate dirty inputs without propagating NaN in calculations.

Overview

Any data that crosses the application boundary (form fields, URL parameters, CSV files) enters as a string. The choice of parser determines whether a dirty input such as '12abc' is clearly rejected (Number) or partially accepted (parseInt / parseFloat).

When calculating totals on textual records (e.g. from CSV), converting and validating before the values enter the .reduce() accumulator prevents a single 'N/A' from poisoning the entire sum becoming NaN.

Sections covered in this lesson

  1. 1Mental model: Number vs parseInt / parseFloat
  2. 2Progressive examples
  3. 3Guided laboratory
  4. 4Check for understanding
Pro Lesson · Module 2 — Strings and numbers

Unlock "Type conversions" and the full JavaScript Course curriculum

Number, String, parseInt, parseFloat, toFixed and when NaN bites you.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in JavaScript Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.