Back to JavaScript Notes
Topic #359

Type Conversion

You will be able to convert JavaScript values between strings, numbers, and booleans on purpose, so your code behaves predictably when working with user input, APIs, and comparisons.

What it is

Type conversion is the process of changing a value from one JavaScript type to another. When you do it deliberately, you are using functions such as Number(), String(), and Boolean(). JavaScript also performs implicit conversion, called coercion, when operators like + or == need values of compatible types.

The mental model is simple: a value has a type, and conversion asks JavaScript to produce a new value of the requested type. For example, Number("42") turns the string "42" into the number 42. Related terms include coercion, parsing, casting, primitive values, and object wrappers.

Why it matters

  • Form inputs and URL parameters arrive as strings, but calculations need numbers.
  • API responses may contain numeric-looking strings that must be converted before math.
  • Explicit conversion makes intent clear and reduces surprising results from implicit coercion.
  • Boolean conversion helps validate whether a value is present or usable.
  • String conversion helps build messages, logs, and user-facing output.

Syntax or steps

The smallest useful pattern is to wrap a value in the conversion function for the type you want:

Number(value);
String(value);
Boolean(value);
BigInt(value);

For strings that contain extra characters, use parseInt() or parseFloat() instead of Number(). Always pass a radix to parseInt(), such as parseInt(value, 10).

Example

const priceText = "19.99";
const quantityText = "3";

const price = Number(priceText);
const quantity = Number(quantityText);
const total = price * quantity;

const message = String(total);
const hasTotal = Boolean(total);

console.log(price);       // 19.99
console.log(quantity);    // 3
console.log(total);       // 59.97
console.log(message);     // "59.97"
console.log(hasTotal);    // true

Part by part:

  • Number(priceText) converts the string "19.99" into the number 19.99.
  • Number(quantityText) converts "3" into the number 3.
  • price * quantity now performs numeric multiplication instead of string concatenation.
  • String(total) converts the number back into a string for display or logging.
  • Boolean(total) converts the number to true because it is not zero, NaN, or another falsy value.

Common mistakes

  • Using Number("42px") and expecting 42. It returns NaN. Use parseInt("42px", 10) if you want the leading number.
  • Forgetting that Boolean("false") is true. Any non-empty string is truthy. Compare the string directly or parse it intentionally.
  • Using + with mixed types. "5" + 2 becomes "52", not 7. Convert first: Number("5") + 2.
  • Using == and relying on coercion. Prefer === and convert values explicitly when needed.

When to use it

Use explicit conversion when you know the source type and need a predictable result. Use implicit coercion only when it is obvious and safe, such as if (value) for presence checks.

GoalUseExampleResult
Convert a clean numeric stringNumber()Number("42")42
Read a number from mixed textparseInt()parseInt("42px", 10)42
Read a decimal from mixed textparseFloat()parseFloat("19.99abc")19.99
Make a value printableString()String(42)"42"
Check truthinessBoolean()Boolean("")false

Practice

Guided exercise: convert " 42 " to a number and add 8.

const answer = Number(" 42 ") + 8;
console.log(answer);

Expected output: 50.

Challenge: convert "10" and "20" to numbers, then multiply them.

Solution hint: use Number() on each string before using *. Expected output: 200.

Quick check

What does Number("42px") return?

It returns NaN, because Number() requires the whole string to be a valid numeric value.

Summary

Type conversion lets you move between strings, numbers, and booleans deliberately. Prefer explicit functions like Number(), String(), and Boolean() when working with external data, and use parsing functions only when the input may contain extra characters.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Type Conversion – FAQs

Quick answers about learning Type Conversion in JavaScript.

This free note from Coding Now Tech Institute explains Type Conversion in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Type Conversion, is 100% free with no signup required.
With focused practice, most students grasp Type Conversion in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now