01 / 21
A B

Explicit
Type Conversion

Master how to intentionally convert between types in JavaScript using Number(), String(), and Boolean().

JavaScript

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:

Number
Numeric values like 42, 3.14, or NaN
String
Text wrapped in quotes like "hello" or "7"
Boolean
Logical values: only true or false

Why 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

Implicit (Coercion) Engine converts for you Hard to predict "5" + 3 → "53" "5" - 3 → 2 vs Explicit (Your Intent) You convert deliberately Always predictable Number("5") + 3 → 8 String(42) → "42"

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:

Number(value)
Converts to a number. Returns NaN if it can't parse.
String(value)
Converts to a string. Always succeeds: every value has a text form.
Boolean(value)
Converts to 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:

Falsy (→ false)
false, 0, "", null, undefined, NaN
Truthy (→ true)
Everything else: "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

1
Identify the incoming type. Use typeof value to check what you have.
2
Determine the target type. What does the next operation expect: a number, a string, or a boolean?
3
Choose the right converter. Use Number(), String(), or Boolean().
4
Handle edge cases. Check for NaN after Number() using Number.isNaN().
5
Use the converted value. Now perform your operation with confidence.

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
Input
"25"
→
Number()
25
→
Result
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

✕ Using new Number()
Adding new creates a wrapper object, not a primitive. typeof new Number(5) is "object", not "number".
✕ Trusting "0" as falsy
Boolean("0") is true because "0" is a non-empty string. Convert to number first if you want numeric falsy behavior.
✕ Confusing null and undefined
Number(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?

Question 2 of 5

What is the result of Boolean("false")?

Question 3 of 5

Which function should you use to convert a number to a text representation?

Question 4 of 5

What does Number(undefined) return?

Question 5 of 5

A form field returns " 99 ". What does Number(" 99 ") evaluate to?