JavaScript Type Conversion
You can name empties with null and undefined. Next skill: change a value’s kind on purpose — turn a number into text for a label, or text into a number for math. That is type conversion. This session sticks to clear helpers: String(), Number(), and Boolean() — plus one common + surprise.
What is type conversion in JavaScript?
Type conversion means turning a value from one kind into another. Prefer explicit helpers so the intent is obvious: String(42) makes "42", Number("7") makes 7, Boolean(0) makes false. If Number cannot parse the text, you get NaN. With +, a number next to a string often concatenates instead of adding — log it once so it sticks.
Conversion toolkit
📊 Type conversion for beginners
| Helper / idea | Example | Result / note |
|---|---|---|
String(...) |
String(42) |
"42" (text) |
Number(...) |
Number("7") |
7 (number) |
Boolean(...) |
Boolean(0) |
false (preview of falsy) |
| Bad number text | Number("hello") |
NaN — still typeof "number" |
| Empty → boolean | Boolean("") |
false |
+ mix |
5 + "2" |
"52" (concat, not 7) |
String() — make text
Wrap a value in String(...) when you need a label or message. Numbers, booleans, null, and undefined all become strings you can join or show.
console.log(String(42));
console.log(String(true));
console.log(String(null));
console.log(String(undefined));
console.log(typeof String(42));
Number() — make a number
Use Number(...) when text should become math-ready. Digit strings convert cleanly. Non-numeric text becomes NaN.
console.log(Number("7"));
console.log(Number("3.5"));
console.log(Number(true));
console.log(Number(false));
console.log(typeof Number("7"));
When Number fails — NaN
Number("hello") is NaN (“Not a Number”). Odd detail from the numbers session: typeof NaN is still "number". Treat NaN as a failed conversion, not a usable count.
console.log(Number("hello"));
console.log(typeof Number("hello"));
console.log(Number(""));
console.log(Number(null));
Number("") and Number(null) become 0 — another beginner trap. Prefer clear digit strings when you mean a real amount.
Boolean() — yes/no from a value
Boolean(...) turns a value into true or false. Empties like 0, "", null, undefined, and NaN become false. Non-empty strings and most numbers become true. Full truthy / falsy lists come next session — here you only need the helper.
console.log(Boolean(1));
console.log(Boolean(0));
console.log(Boolean("hi"));
console.log(Boolean(""));
console.log(Boolean(null));
The + surprise — concat vs add
If one side of + is a string, JavaScript often joins text instead of adding numbers. Convert first with Number() when you mean math.
console.log(5 + 2);
console.log(5 + "2");
console.log(Number("5") + Number("2"));
console.log("Score: " + 10);
Show conversions on the page
Convert a few values, then write short labels with textContent.
<!DOCTYPE html>
<html>
<head>
<title>Type conversion — labels</title>
</head>
<body>
<p id="out">…</p>
<script>
let raw = "12";
let asNumber = Number(raw);
let asText = String(asNumber);
let flag = Boolean(asNumber);
document.getElementById("out").textContent =
"raw=" + raw +
" | number=" + asNumber +
" | text=" + asText +
" | flag=" + flag;
</script>
</body>
</html>
The paragraph shows the same idea in three kinds. Change raw to "0", refresh, and watch flag become false while the number is still 0.
Try it yourself
- Log
String(99),String(false), andtypeof String(99). - Log
Number("8")andNumber("8") + 2— expect math, not"82". - Log
Number("oops")andtypeofof that result (NaN, still"number"). - Log
Boolean("")andBoolean("ok"). - Log
3 + "4"then rewrite withNumber("3") + Number("4"). - In the HTML demo, set
rawto"0", refresh, and read the newflag.
Common mistakes
- Assuming
+always adds — a string on either side can concatenate - Ignoring
NaNafterNumber("hello")and using it in math anyway - Thinking this session replaces truthy / falsy —
Boolean()is only a preview - Expecting
String(null)to be""— it is the text"null" - Forgetting to check
typeofafter a conversion when results look odd
Summary
- Type conversion: change a value’s kind on purpose
- Prefer
String(),Number(),Boolean()for clear intent - Bad numeric text →
NaN;typeof NaNis still"number" +with a string often concatenates — convert before you add- Next: truthy and falsy — which values act like true or false in conditions
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top