Skip to content

Commit b282c1e

Browse files
committed
Debugging Challenge with examples
1 parent ce92942 commit b282c1e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

explicit-and-implicit-conversion-in-javascript.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ Use console.log() to clearly show the before-and-after type conversions.
1919
*/
2020

2121

22-
let result = "5" - 2;
22+
// Explicitly converting string to number before performing arithmetic
23+
let result = Number("5") - 2;
2324
console.log("The result is: " + result);
2425

25-
let isValid = Boolean("false");
26+
// Using explicit Boolean conversion for a string
27+
let isValid = Boolean("false"); // This will be true because non-empty strings are truthy
2628
if (isValid) {
2729
console.log("This is valid!");
2830
}
2931

32+
// Explicitly converting the string to a number for arithmetic operations
3033
let age = "25";
31-
let totalAge = age + 5;
34+
let totalAge = Number(age) + 5;
3235
console.log("Total Age: " + totalAge);
36+

0 commit comments

Comments
 (0)