-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09.js
More file actions
25 lines (20 loc) · 694 Bytes
/
Copy path09.js
File metadata and controls
25 lines (20 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 🔁 Type Conversion (Manual)
// String to Number
let str = "123";
let num = Number(str); // 123
console.log(typeof num); // "number"
// Number to String
let n = 456;
let str2 = String(n); // "456"
console.log(typeof str2); // "string"
// Boolean to Number
console.log(Number(true)); // 1
console.log(Number(false)); // 0
// Number to Boolean
console.log(Boolean(0)); // false
console.log(Boolean(123)); // true
// 🔁 Type Coercion (Automatic)
console.log("5" + 2); // "52" → string + number = string
console.log("5" - 2); // 3 → string converted to number
console.log("5" * "2"); // 10 → both converted to number
console.log(1 + true); // 2 → true is 1