Module lessons (4/4)
Type conversions
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)andparseFloat(str). - Know the Boolean conversion rules (
Boolean(x)) and the critical falsehoods ('',0). - Recognize the cases in which
NaNarises and distinguish it from finite values withNumber.isNaNandNumber.isFinite. - Write defensive parsers that validate dirty inputs without propagating
NaNin 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
- 1Mental model:
NumbervsparseInt/parseFloat - 2Progressive examples
- 3Guided laboratory
- 4Check for understanding
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.