Explicit
Type Conversion
Master how to intentionally convert between types in JavaScript using Number(), String(), and Boolean().
What Are Data Types?
Every value in JavaScript has a type: it tells the engine how to store the value in memory and what operations are valid on it. The three most common primitive types are:
42, 3.14, or NaN"hello" or "7"true or falseWhy Convert Types?
JavaScript is dynamically typed: a variable can hold any type, and the engine sometimes converts types for you implicitly. This causes subtle bugs. Explicit conversion means you do it yourself, on purpose, so behavior is predictable.
// Implicit (dangerous surprise!) "5" + 3 // "53" (string concat, not math!) // Explicit (clear intent) Number("5") + 3 // 8 (proper addition)
Implicit vs Explicit
The Three Converter Functions
JavaScript provides three built-in functions that explicitly convert any value to a specific type. Each is a global function called without new:
NaN if it can't parse.true or false based on "truthy/falsy" rules.Deep Dive: Number()
Number("42") // 42 Number("3.14") // 3.14 Number(" 99 ") // 99 (trims whitespace) Number("") // 0 (empty string → 0) Number("hello") // NaN (not a number) Number(true) // 1 Number(false) // 0 Number(null) // 0 Number(undefined) // NaN
Deep Dive: String()
String(42) // "42" String(3.14) // "3.14" String(true) // "true" String(false) // "false" String(null) // "null" String(undefined) // "undefined" String(NaN) // "NaN" String([1,2,3]) // "1,2,3"
Deep Dive: Boolean()
JavaScript defines exactly six falsy values. Everything else is truthy:
false, 0, "", null, undefined, NaN"0", "false", [], {}, -1, " "Boolean(0) // false Boolean("") // false Boolean("0") // true (non-empty string!) Boolean("false") // true (non-empty string!) Boolean([]) // true (object, not empty string)
Explicit vs Implicit Side by Side
| Expression | Implicit Result | Explicit Approach |
|---|---|---|
"5" + 3 |
"53" |
Number("5") + 3 → 8 |
"5" - 3 |
2 |
Number("5") - 3 → 2 |
true + 1 |
2 |
Number(true) + 1 → 2 |
"" == false |
true |
Boolean("") === false |
How to Apply Explicit Conversion
typeof value to check what you have.Number(), String(), or Boolean().NaN after Number() using Number.isNaN().Example 1: Form Input Math
A user types "25" into an age field. HTML form inputs always return strings. We need a number to calculate birth year.
let ageInput = "25"; // from the form let currentYear = 2025; // Step 1: Convert string → number let age = Number(ageInput); // 25 (number) // Step 2: Perform arithmetic safely let birthYear = currentYear - age; console.log(birthYear); // 2000
Example 2: Building Messages
You have a numeric score and need to display it in a message string. Without explicit conversion, the + operator would still work here (implicit), but being explicit makes your intent clear.
let score = 98; let passed = true; // Explicit conversions let scoreText = String(score); // "98" let passedText = String(passed); // "true" let msg = "Score: " + scoreText + ", Passed: " + passedText; console.log(msg); // "Score: 98, Passed: true"
Example 3: Validating User Input
Check whether a user entered anything in a text field. The field returns "" (empty string) when blank. Use Boolean() to convert to a clean true/false check.
let username = ""; // user left it blank let email = "a@b.com"; if (Boolean(username)) { console.log("Username provided"); } else { console.log("Username is missing!"); // ← this runs } if (Boolean(email)) { console.log("Email provided"); // ← this runs }
Handling NaN Safely
When Number() cannot parse a value, it returns NaN (Not a Number). NaN is toxic: any math with it produces more NaN.
let price = Number("free"); // NaN price + 10; // NaN (spreads!) // Guard against NaN: if (Number.isNaN(price)) { console.log("Invalid price!"); } else { console.log("Total: " + (price + 10)); }
Common Mistakes
new Number()new creates a wrapper object, not a primitive. typeof new Number(5) is "object", not "number"."0" as falsyBoolean("0") is true because "0" is a non-empty string. Convert to number first if you want numeric falsy behavior.null and undefinedNumber(null) is 0, but Number(undefined) is NaN. They look similar but convert differently.Conversion Reference Table
| Input Value | Number() | String() | Boolean() |
|---|---|---|---|
"42" |
42 | "42" | true |
"" |
0 | "" | false |
true |
1 | "true" | true |
false |
0 | "false" | false |
null |
0 | "null" | false |
undefined |
NaN | "undefined" | false |
"hello" |
NaN | "hello" | true |
Question 1 of 5
What does Number("") return?
"" is treated as 0 by Number(). This is a special rule: empty strings and whitespace-only strings convert to 0, not NaN.Question 2 of 5
What is the result of Boolean("false")?
"false" is a non-empty string, so Boolean("false") returns true. Only the empty string "" is falsy. The text content does not matter: any non-empty string is truthy.Question 3 of 5
Which function should you use to convert a number to a text representation?
String() converts any value to its text form. String(42) returns "42". parseInt() does the opposite (string to number), and the others do not produce strings.Question 4 of 5
What does Number(undefined) return?
Number(undefined) returns NaN. This is different from Number(null), which returns 0. This null vs undefined distinction is a classic gotcha in JavaScript type conversion.Question 5 of 5
A form field returns " 99 ". What does Number(" 99 ") evaluate to?
Number() automatically trims whitespace before parsing. So Number(" 99 ") first sees "99" then converts to the number 99. No NaN here.