SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
Lesson 23 of 30

JavaScript Type Conversion

5 min read · Free · No login needed

JavaScript is a loose language. A value can be a number one minute and a string the next, and JavaScript will happily change it for you — sometimes when you did not ask. That is called type conversion. You need it all the time, because the most common source of values — a form on a web page — always gives you strings, even when the user types a number.

Diagram — see how it fits together
Diagram: JavaScript Type Conversion

Think of a chai shop order form. The customer types 3 in the quantity box. JavaScript reads it as the string "3", not the number 3. If you add "3" + 1, you get "31" — the string and the number glued together. If you convert first with Number("3"), you get 3 + 1 = 4, the answer you wanted. One small conversion, one big difference.

Converting on purpose — Number, String, Boolean

JavaScript gives you three simple tools for changing a type on purpose:

  • Number("42") turns a string into a number: 42. Number("hello") cannot be a number, so it gives NaN — "Not a Number". Number("") gives 0, and Number(true) gives 1.
  • String(42) turns a number into a string: "42". String(true) gives "true". You use it when you want to join a value into a message.
  • Boolean(1) gives true. Boolean(0), Boolean(""), Boolean(null) and Boolean(undefined) all give false — these are the falsy values. Everything else is truthy, including the string "0" and an empty array [].

You can also use shortcuts: +"42" (a plus sign in front) works like Number("42"), and "" + 42 works like String(42). They are short, but for a beginner the named functions are clearer — use them until the shortcuts feel natural.

Note: NaN is strange: it is not equal to anything, not even itself. NaN === NaN is false. To check whether a value is NaN, use Number.isNaN(value) — never compare it with ===.

== vs === — the question that decides interviews

JavaScript has two equality signs, and they are not the same. == (loose equality) converts the types first, then compares: "5" == 5 is true, because the string "5" becomes the number 5. === (strict equality) checks the type and the value, with no conversion: "5" === 5 is false, because a string is not a number.

Always use === unless you have a clear reason not to. Loose equality hides bugs — a string that looks like a number passes a check it should fail, and the mistake shows up far away from where it was made. The same rule applies to != and !==: prefer !==.

Tip: Remember it like this: === checks the face AND the name — type and value both. == only checks the face and lets the name slide. In real code and in interviews, === is the safe answer.

Truthy and falsy — the short list to memorise

When JavaScript needs a true/false answer — inside an if, for example — it converts the value with the Boolean rules. Only six values are falsy: false, 0, "" (empty string), null, undefined, and NaN. Everything else is truthy. That means if ("hello") runs, if (0) does not, and if ("0") runs — because "0" is a non-empty string, and non-empty strings are truthy.

This is useful for defaults. let name = input || "Guest"; gives "Guest" when the input is empty, because an empty string is falsy and the || moves on to the next value. You will see this pattern everywhere in real code.

Common mistakes

  • Adding a form value without converting it — "3" + 1 is "31", not 4. Convert with Number() first.
  • Using == and wondering why "5" == 5 is true — use === unless you truly want conversion.
  • Checking value === NaN — it is always false. Use Number.isNaN(value).
  • Thinking "0" is falsy — it is a non-empty string, so it is truthy. Only the empty string "" is falsy.
  • Trusting Number("42px") — it gives NaN. parseInt("42px") gives 42, because it reads the number at the start and stops at the letters.

Remember

  • Number(), String() and Boolean() convert a value on purpose.
  • === checks type AND value; == converts first. Prefer ===.
  • Only six values are falsy: false, 0, "", null, undefined, NaN.
  • Form inputs are always strings — convert them before doing maths.
  • === checks the face AND the name — type and value both.

Example explained

  • <input id="qty" value="3"> — the quantity box. Whatever the user types, JavaScript reads it as a string.
  • let qty = Number(document.getElementById("qty").value); — converts the string "3" into the number 3, so the maths works.
  • let total = qty * 10; — 3 cups at ₹10 each is ₹30. Multiplication would convert the string by itself, but addition would not — so converting first keeps every operation safe.
  • if (qty === 0) — strict equality: if the user typed 0, the number 0 matches, but the string "0" would not have matched before conversion.
  • document.getElementById("out").textContent = "Total: ₹" + total; — shows the answer on the page. Here the + joins strings on purpose.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Chai Bill — Type Conversion</title>
<style>
body { font-family: Verdana, sans-serif; background-color: #fff8e1; }
h1 { color: #b45309; text-align: center; }
.card { background-color: white; border: 2px solid #f59e0b; padding: 16px; width: 340px; margin: 20px auto; }
button { background-color: #b45309; color: white; border: none; padding: 10px 18px; font-size: 16px; }
#out { font-weight: bold; color: green; }
</style>
</head>
<body>

<h1>Chai Bill</h1>
<div class="card">
<p>How many cups? <input id="qty" type="text" value="3"></p>
<p><button onclick="makeBill()">Make my bill</button></p>
<p id="out">Your total will appear here.</p>
<p>Try it: type 3 and click the button — the total is ₹30, not "31".</p>
</div>

<script>
function makeBill() {
  let qty = Number(document.getElementById("qty").value);
  let total = qty * 10;
  if (qty === 0) {
    document.getElementById("out").textContent = "You ordered 0 cups — nothing to pay!";
  } else {
    document.getElementById("out").textContent = "Total: ₹" + total;
  }
}
</script>

</body>
</html>

Output

Output — how it looks in the browser
Output of JavaScript Type Conversion

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 30 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Advanced Cyber Security Industrial Training
₹5,000
View
Full Stack PHP Development Course 2026
₹3,000
View
Core Python Programming – 3 Month Complete Course
₹700
View
Browse all courses
JavaScript — Lessons
1. JavaScript Introduction 2. JavaScript Where To 3. JavaScript Output 4. JavaScript Syntax 5. JavaScript Comments 6. JavaScript Variables 7. JavaScript Operators 8. JavaScript Data Types 9. JavaScript Functions 10. JavaScript Objects 11. JavaScript Scope 12. JavaScript Events 13. JavaScript Strings 14. JavaScript Numbers 15. JavaScript Arrays 16. JavaScript Array Methods 17. JavaScript Dates 18. JavaScript Math 19. JavaScript Conditions 20. JavaScript Switch 21. JavaScript For Loops 22. JavaScript While Loops 23. JavaScript Type Conversion 24. JavaScript Errors — try, catch, finally 25. JavaScript JSON 26. JavaScript Arrow Functions 27. JavaScript Classes 28. JavaScript Promises and async/await 29. JavaScript and the DOM 30. JavaScript Form Validation
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🛡️ 7-Day Money-Back Guarantee
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Download karne ke liye login karo. Login ke baad yahi page pe rahoge!

Naya ho? Register karo →