Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 05 / 34

Coercion & explicit conversion

01

Understand the idea

A quantity field contains 2, but adding one produces 21. Input values are strings, and + can join strings.

Choose the adapter deliberately

A browser form gives you text even when the person typed digits. Your code needs a number for a quantity calculation and text again for a label. Conversion exists because these boundaries are normal, not because the user entered something wrong.

Why form input surprises people

Start by inspecting the type you actually received. A numeric-looking input is still a string when read through an input element’s value . If a calculation produces unexpected text, inspect the values before the operator instead of assuming the operator itself is broken.

The + operator can add numbers or join strings. With the text "2" on one side, + 1 produces "21". A subtraction such as "2" - 1 instead asks for numeric conversion. That inconsistency is why converting once at the input boundary is easier to read than hoping each operator does the right thing.

Conversion and comparison choices
ChoiceWhat it doesA useful warning
Number(text)Converts the entire value to a numberAn empty or whitespace-only string becomes 0
parseFloat(text)Reads a numeric prefix"12px" becomes 12, which may hide bad input
String(value)Explicitly produces textUseful at a display boundary
Boolean(value)Uses truthiness rulesThe non-empty string "false" becomes true
=== and !==Compare without type coercionUsually the clearest default
== and !=May convert types while comparingDifferent types can compare equal

Turning text into numbers

Number asks whether the whole input can represent a number. parseFloat reads a numeric prefix, so the text "12px" becomes 12. That can be useful when reading a known formatted value, but wrong when validating a quantity field. Choose deliberately whether extra characters are acceptable.

Choosing an equality rule

Strict equality, ===, compares without converting the operands to a common type. Loose equality, ==, applies conversion rules for different types. Prefer strict equality for ordinary comparisons and convert deliberately before comparing. Neither operator compares separate by their contents.

Validate before calculating

Conversion is not validation. Number("") is zero, while Number("two") is NaN. Check whether input is blank before conversion if blank is invalid, then use Number.isFinite for a finite numeric value or Number.isNaN when specifically checking . is another conversion: the string "false" is truthy because it is not empty.

Decide what counts as acceptable input before converting it. For a quantity, you might require non-empty text, a finite result, an integer, and a value at least zero. Number.isFinite rules out both NaN and Infinity. Conversion answers “what number does this become?”; validation answers “is that number allowed here?”

02

Mental model

FOLLOW THE VALUE

"2"
string + 1
"21"

Without conversion, addition joins text.

01 / 03Trace the transformation

03

Live example

The first log is arithmetic after conversion. The next two compare the same string and number under different equality rules.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Add numerically

Convert the raw quantity before adding one. Preserve the two comparison logs so you can see their different rules.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

3
false
true

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does Number("") return?
    02Why is "2" === 2 false?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson