Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.5 — Type Conversion
JavaScript · Example 1 of 4

Explicit conversion with String, Number and Boolean

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.

01

Convert the strings "19.5" and "0.5" to numbers and log their sum, which must be 20.

02

Read the width out of "240px" as a number, double it, and log 480px.

03

Predict, then check: "7" * "6", "7" + "6", "7" - - "6".

Explain it without notes

01

What is the difference between explicit conversion and coercion? Give an example of each.

02

When would you use Number() and when parseInt()/parseFloat()?

03

Explain the outputs of "5" + 1, "5" - 1 and 1 + 2 + "3".

04

Which values are falsy?

Explicit conversion with String, Number and Boolean

Console

(nothing was printed)

Expected console output

42 true null undefined
1,2,3 [object Object]
ff 101
42 12 0 NaN
1 0 0 NaN
26 0 5 NaN
3.5
false true false true true
true false