Form values are strings
Type a different number (or a word) in the box and press the button. The ? : used here is the ternary operator from Topic 2.3.
JavaScript converts values between types either when you ask (String(), Number(), Boolean(), parseInt, parseFloat) or automatically when an operator needs a different type; knowing the automatic rules prevents bugs like "5" + 1 giving "51".
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Convert the strings "19.5" and "0.5" to numbers and log their sum, which must be 20.
Read the width out of "240px" as a number, double it, and log 480px.
Predict, then check: "7" * "6", "7" + "6", "7" - - "6".
Explain it without notes
What is the difference between explicit conversion and coercion? Give an example of each.
When would you use Number() and when parseInt()/parseFloat()?
Explain the outputs of "5" + 1, "5" - 1 and 1 + 2 + "3".
Which values are falsy?
Preview
Console
Nothing logged yet.Expected console output
string
51
6