FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Values & Types

Intermediate

Type Conversion

Type conversion happens when JavaScript changes a value from one type to another. Explicit conversion is clear. Implicit coercion can be convenient or dangerous.

Number("75");   // 75
String(75);     // "75"
Boolean("");    // false

"75" + 5;       // "755"

Values & Types

Convert values at the boundary, not randomly in the middle of logic.

JavaScript is dynamically and weakly typed. A variable can hold different value types over time, and some operators convert values automatically.

The plus operator is the classic trap. If one side is a string, + may concatenate instead of add. Other numeric operators usually push values toward numbers.

Professional code converts input deliberately. Form values start as strings. API data may not be shaped how you expect. Convert once, validate, then work with predictable values.

Explicit conversion

Use Number(), String(), Boolean(), BigInt() or parsing functions when you mean conversion.

Implicit coercion

JavaScript converts automatically in many operators and comparisons.

Form boundary

Inputs from the DOM are usually strings and need conversion before math.

Validation

Conversion should be followed by checking NaN, range and meaning.

Examples

Good type handling makes JavaScript predictable.

Convert form input before calculating

const rawItems = input.value;
const itemCount = Number(rawItems);

if (Number.isNaN(itemCount)) {
  showError("Enter a valid number.");
} else {
  showTotal(itemCount * 12);
}

Letting + decide your data model

const itemCount = input.value;
const total = itemCount + 75;

console.log(total); // "1575" when input.value is "15"

Rules that matter

Types are not paperwork. They are behavior.

JavaScript lets values move flexibly, but professional code keeps meaning clear. Convert at boundaries, validate input and choose values that represent the real idea.

Convert at boundaries

Convert form, URL, localStorage and API values before business logic.

Check NaN after Number

Number("abc") is NaN, and NaN poisons calculations.

Use strict equality by default

=== avoids many coercion surprises from ==.

Use parseInt carefully

Always know whether you want integers, decimals or full-number conversion.

Avoid clever coercion tricks

+value and !!value are compact, but often less readable for beginners.

Keep types stable

Once a value is normalized, do not let it bounce between string and number.

Production thinking

Data quality starts with value quality.

Why does this matter?

Type conversion bugs are quiet. The page still runs, but the total, label or condition is wrong. Explicit conversion makes intent visible.

Accessibility

Validation messages depend on correct conversion. Users need clear feedback when input is invalid, not silent wrong totals.

Production note

Production code should normalize input once and validate it before deeper logic. This reduces bugs across forms, APIs and storage.

SEO note

Wrong type conversion can render wrong prices, counts, titles or structured values.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Remove Number() and see how + behaves if you add instead of multiply.
  • Type abc into the input and inspect the validation message.
  • Change Number() to parseInt() and test decimal input.

Practice assignment

Do this before moving to the next topic.

  1. Convert a string input to a number.
  2. Validate it with Number.isNaN.
  3. Write one example where || would create a wrong fallback.

Try it yourself

Convert input before math

Live preview

Self-check

Before you continue, prove that you understand Type Conversion.

Intermediate

Say the answer in plain language. If you can explain the value behavior, the code becomes much easier to debug.

  1. Can you explain explicit conversion?
  2. Can you explain implicit coercion?
  3. Can you explain why form values need conversion?
  4. Can you explain why + is risky with strings?
  5. Can you explain why strict equality is the default choice?