-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathexplicit-and-implicit-conversion-in-javascript.js
More file actions
55 lines (40 loc) · 1.57 KB
/
explicit-and-implicit-conversion-in-javascript.js
File metadata and controls
55 lines (40 loc) · 1.57 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
Part 1: Debugging Challenge
Original Code and Unexpected Behavior
javascript
let result = "5" - 2;
console.log("The result is: " + result); // Outputs: 3
let isValid = Boolean("false");
if (isValid) {
console.log("This is valid!"); // Outputs: This is valid! (unexpected)
}
let age = "25";
let totalAge = age + 5;
console.log("Total Age: " + totalAge); // Outputs: Total Age: 255 (unexpected)
Fixed Code with Comments
javascript
// "5" - 2 works because JavaScript implicitly converts "5" to a number.
// No fix needed here.
let result = Number("5") - 2;
console.log("The result is: " + result); // Outputs: 3
// Boolean("false") returns true because any non-empty string is truthy.
// To fix this, we need to check the actual string value.
let isValid = ("false" === "true"); // Explicit comparison instead of Boolean conversion
if (isValid) {
console.log("This is valid!");
} else {
console.log("This is NOT valid!"); // Correct output
}
// "25" + 5 results in string concatenation: "255"
// Fix by converting "25" to a number first
let age = "25";
let totalAge = Number(age) + 5;
console.log("Total Age: " + totalAge); // Outputs: 30
Your Own Examples
Implicit Type Conversion
javascript
let value = "10" * 2; // "10" is implicitly converted to number
console.log("Implicit conversion result:", value); // Outputs: 20
let input = undefined;
console.log("Before conversion:", input, "Type:", typeof input);
let converted = String(input); // Converts undefined to "undefined"
console.log("After conversion:", converted, "Type:", typeof converted); // Outputs: "undefined", string