Learning object 6 of 100
Type conversion explorer
Enter any value and compare every common conversion side by side: Number, parseInt, parseFloat, unary +, String, Boolean, and the implicit conversions done by + and *.
Loading…
What it shows
Explicit conversions (Number, String, Boolean) say what you mean. Implicit ones happen inside operators: + prefers strings when either side is text, while *, - and / always convert to numbers. parseInt and parseFloat read digits from the start of text and stop at the first character that does not fit.
Guided experiment
- Compare
"42px"with Number and parseInt. Which one gives NaN? - Try
""and" 12 ". Why is Number("") a trap for form validation? - Compare
[]and[5]. What does each become as a number and as a boolean?
Predict
What is "5" * 1 + "5"?
Related
Lessons: Lesson 10: Type conversion and type checking
Projects: Project 1: Interactive Tip Calculator
Source: web/assets/js/objects/object-006.js in the companion repository.