let numberAge = Number(age);
console.log(numberAge);
Now:
25 is a number.
8️⃣ Converting to String
Use "String()".
let age = 25;
let textAge = String(age);
console.log(typeof textAge);
Output:
string
9️⃣ Converting to Boolean
Use "Boolean()".
console.log(Boolean(1)); // Output: true
console.log(Boolean(0)); // Output: false
Some common falsy values are:
• false
• 0
• ""
• null
• undefined
• NaN
Most other values are truthy.
Understanding truthy and falsy values will become very useful when writing conditions.
🔟 Type Coercion
JavaScript can sometimes automatically convert values between types.
For example:
console.log("5" + 2);Output:
``
text
52``Why?
Because
+
with a string causes the number to be converted into a string.
But:
console.log("5" - 2);Output:
``
text
3``JavaScript converts
"5"
into a number for the subtraction.
This behavior is called type coercion.
🚀 Double Tap ❤️ For Part-3