Skip to content

Commit 88bd9ec

Browse files
committed
Used explicit type conversion to fix bugs; write my own examples of implicit and explicit type conversions
1 parent ce92942 commit 88bd9ec

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

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

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

2121

22-
let result = "5" - 2;
22+
let result = Number("5") - 2;
2323
console.log("The result is: " + result);
24+
//Converted result from String to Number
2425

25-
let isValid = Boolean("false");
26+
let isValid = Boolean(false);
2627
if (isValid) {
2728
console.log("This is valid!");
2829
}
30+
//Removed quotation marks to make isValid falsy rather than truthy
2931

3032
let age = "25";
31-
let totalAge = age + 5;
33+
let totalAge = Number(age) + 5;
3234
console.log("Total Age: " + totalAge);
35+
//Converted age from String to Number to make age evaluate as a Number
36+
37+
//My own example of implicit type conversion
38+
let implicitTypeConversion
39+
if (!implicitTypeConversion) {
40+
console.log("This is invalid!");
41+
}
42+
43+
//My own example of explicit type conversion
44+
let explicitTypeConversion
45+
if (!Boolean(explicitTypeConversion)) {
46+
console.log("This is invalid!");
47+
}

0 commit comments

Comments
 (0)