diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index ede0ccd..2dd4be4 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -20,13 +20,25 @@ Use console.log() to clearly show the before-and-after type conversions. let result = "5" - 2; -console.log("The result is: " + result); +let numResult = Number(result); +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. let isValid = Boolean("false"); if (isValid) { - console.log("This is valid!"); + console.log("This is valid!"); // This is correct. Any non-empty string is considered truthy, even the string "false". } let age = "25"; -let totalAge = age + 5; -console.log("Total Age: " + totalAge); +let numAge = Number(age); +let totalAge = numAge + 5; +console.log("Total Age: " + totalAge); // Strings and numbers are concatenated by the + operator. To fix, I converted the sting "age" into a number. + +let redDucks = Boolean(null); +if (redDucks) { + console.log("Ducks are red!"); +} else { + console.log("Ducks aren't red!"); // Example of explicit conversion, converting 0 to false. Also edge case because I used null. +} + +let myAge = "My age is " + 35; // Example of implicit conversion, STRING CONCATENATION WITH THE + OPERATOR. +console.log(myAge); \ No newline at end of file