Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path008-type-conversion.js
More file actions
Latest commit
120 lines (97 loc) · 3.55 KB
/
Copy path008-type-conversion.js
File metadata and controls
120 lines (97 loc) · 3.55 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 1: WHAT IS TYPE CONVERSION?
// 2: EXPLICIT CONVERSION
// Converting to String
// const num = 42;
// const str = String(num);
// const str = num.toString();
// console.log(str); // "42"
// console.log(typeof str) // string
// Converting to Number
// const str = "42";
// const num = Number(str);
// console.log(num); // 42
// console.log(typeof num) // number
// const invalid = Number('hello')
// console.log(invalid) // NaN (Not a number)
// const str = "123px";
// const num = parseInt(str)
// console.log(num);
// const str = "5.99";
// const num = parseFloat(str)
// console.log(num); // 5.99
// const str = "456";
// const num = +str;
// console.log(num); // 456
// Converting to Boolean
// - false
// - 0
// - ""
// - null
// - undefined
// - NaN
// console.log(Boolean(0)); // false
// console.log(Boolean("")); // false
// console.log(Boolean(null)); // false
// console.log(Boolean(undefined)); // false
// console.log(Boolean(NaN)); // false
// console.log(Boolean(1)); // true
// console.log(Boolean("hello")); // true
// console.log(Boolean([])); // true (even empty array!)
// console.log(Boolean({})); // true (even empty object!)
// 3: IMPLICIT CONVERSION
// String Coercion
// const result = "The answer is " + 50;
// console.log(result)
// Numeric Coercion
// console.log("10" - 5); // 5
// console.log("10" * "2"); // 20
// console.log("20" / "4"); // 5
// Boolean Coercion
// if ("hello") {
// console.log("This runs!"); // This runs because "hello" is truthy
// }
// if (0) {
// console.log("This won't run"); // Won't run because 0 is falsy
// }
// 4: COMMON PITFALLS & BEST PRACTICES
// console.log("5" + 3 + 2); // "532" - left to right, string concatenation
// console.log(3 + 2 + "5"); // "55" - adds first, then concatenates
// console.log("5" - "2"); // 3 - subtraction forces numeric conversion
// console.log("5" * "2"); // 10
// console.log("five" * 2); // NaN - can't convert "five" to number
// let userInput = "123abc";
// let number = Number(userInput); // NaN
// if (isNaN(number)) {
// console.log("Invalid number!");
// } else {
// console.log("Valid number:", number);
// }
// 5: PRACTICAL EXAMPLE
// User enters values as strings
letinput1="25";
letinput2="10";
// Wrong way - implicit conversion leads to concatenation
letwrong=input1+input2;
console.log(wrong);// "2510"
// Right way - explicit conversion
letnum1=Number(input1);
letnum2=Number(input2);
letcorrect=num1+num2;
console.log(correct);// 35
// Even better - with validation
functionsafeAdd(a,b){
letnum1=Number(a);
letnum2=Number(b);
if(isNaN(num1)||isNaN(num2)){
return"Invalid input!";
}
returnnum1+num2;
}
console.log(safeAdd("25","10"));// 35
console.log(safeAdd("hello","10"));// "Invalid input!"
// 6: CONCLUSION
// - Explicit conversion: uses String(), Number(), Boolean(), parseInt(), parseFloat()
// - Implicit conversion: happens automatically with operators and conditions
// - Always be explicit when possible to avoid bugs
// - Remember the falsy values: false, 0, "", null, undefined, NaN
// - Validate conversions, especially with user input