Skip to content

Commit d542752

Browse files
committed
Examples of Type Conversion
1 parent 7207c54 commit d542752

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Use console.log() to clearly show the before-and-after type conversions.
2020

2121

2222
let result = "5" - 2;
23-
let numResult = Number(result)
23+
let numResult = Number(result);
2424
console.log("The result is: " + numResult); // While the original code did get the correct output due to implicit conversion, this gets it through excplicit conversion.
2525

2626
let isValid = Boolean("false");
@@ -32,3 +32,13 @@ let age = "25";
3232
let numAge = Number(age);
3333
let totalAge = numAge + 5;
3434
console.log("Total Age: " + totalAge); // Strings and numbers are concatenated by the + operator. To fix, I converted the sting "age" into a number.
35+
36+
let redDucks = Boolean(null);
37+
if (redDucks) {
38+
console.log("Ducks are red!");
39+
} else {
40+
console.log("Ducks aren't red!"); // Example of explicit conversion, converting 0 to false. Also edge case because I used null.
41+
}
42+
43+
let myAge = "My age is " + 35;
44+
console.log(myAge);

0 commit comments

Comments
 (0)