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 number19.99.Number(quantityText)converts"3"into the number3.price * quantitynow 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 totruebecause it is not zero,NaN, or another falsy value.
Common mistakes
- Using
Number("42px")and expecting42. It returnsNaN. UseparseInt("42px", 10)if you want the leading number. - Forgetting that
Boolean("false")istrue. Any non-empty string is truthy. Compare the string directly or parse it intentionally. - Using
+with mixed types."5" + 2becomes"52", not7. 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.
| Goal | Use | Example | Result |
|---|---|---|---|
| Convert a clean numeric string | Number() | Number("42") | 42 |
| Read a number from mixed text | parseInt() | parseInt("42px", 10) | 42 |
| Read a decimal from mixed text | parseFloat() | parseFloat("19.99abc") | 19.99 |
| Make a value printable | String() | String(42) | "42" |
| Check truthiness | Boolean() | 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.