JavaScript

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

JavaScript

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?


JS Type Conversion Quiz

Q1: What does String(100) return?

A. 100
B. "100"
C. undefined
D. NaN

Q2: What is the result of "10" - 3?

A. "7"
B. NaN
C. 7
D. "103"

Q3: Which of the following is a falsy value in JavaScript?

A. "false"
B. 1
C. true
D. 0

Q4: What does Boolean("0") return?

A. false
B. true
C. "true"
D. "false"

Q5: Which method will convert "3.14" to 3.14 as a number?

A. Number()
B. parseFloat()
C. parseInt()
D. toFloat()

Q6: What will be the result of Number("abc")?

A. 0
B. abc
C. undefined
D. NaN

Q7: What will Boolean("") return?

A. false
B. true
C. ""
D. undefined

Q8: Which of the following will result in string concatenation?

A. "4" - 2
B. "4" * 2
C. "4" + 2
D. 4 + 2

Q9: Which function converts any value to a string safely, even if it's null or undefined?

A. value.toString()
B. toString(value)
C. String(value)
D. value + ""

Q10: What is the type of result after parseInt("45.67")?

A. "string"
B. number
C. float
D. NaN

Go Back Top