JavaScript

coding learning websites codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Type Conversion


📘 JavaScript Type Conversion – Changing Data Types

JavaScript is a dynamically typed language, meaning variables are not bound to a specific data type. Sometimes JavaScript automatically converts values to another type (implicit conversion), and sometimes you must convert them manually (explicit conversion).


🔹 1. Implicit Type Conversion (Type Coercion)

JavaScript automatically converts data types when performing operations involving different types.

let result = "5" + 2;     // "52" → number 2 is converted to string
let value = "10" - 3;     // 7   → string "10" is converted to number

📌 Rules:

  • + operator with a string → converts everything to a string

  • Other arithmetic operators (-, *, /) → convert strings to numbers (if possible)


🔹 2. Explicit Type Conversion

You can manually convert one type to another using built-in functions:

Function Converts to Example Result
String(value) String String(123) "123"
Number(value) Number Number("456") 456
Boolean(value) Boolean Boolean(1) true

🔸 Convert to String
let num = 100;
String(num);         // "100"
num.toString();      // "100"

🔸 Convert to Number
let str = "42";
Number(str);         // 42
parseInt("42.5");    // 42
parseFloat("42.5");  // 42.5

🔸 Convert to Boolean
Boolean(0);          // false
Boolean("");         // false
Boolean("hello");    // true
Boolean(123);        // true

⚠️ Falsy and Truthy Values
Falsy:
  • 0

  • "" (empty string)

  • null

  • undefined

  • NaN

  • false

Everything else is considered truthy.


Practice Questions

Q1. How do you explicitly convert a number 123 to a string using String()?

Q2. What is the result of "5" + 10 and why does it happen?

Q3. How do you convert the string "42" to a number and store it in a variable num?

Q4. How do you check the boolean value of a non-empty string like "hello"?

Q5. What is the result of Boolean(0) and what does it represent?

Q6. How do you convert the string "3.14" to a float using parseFloat()?

Q7. How do you use toString() to convert a boolean true to a string?

Q8. How do you convert the number 10 into a boolean?

Q9. What happens if you use Number("abc")? What is the output?

Q10. How do you convert an empty string "" to a boolean and what is the result?


JavaScript

online coding class codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top