From 88bd9ec72776f07e7884968791ea1067469cbfac Mon Sep 17 00:00:00 2001 From: gweseloh Date: Thu, 6 Mar 2025 09:17:34 -0600 Subject: [PATCH] Used explicit type conversion to fix bugs; write my own examples of implicit and explicit type conversions --- ...t-and-implicit-conversion-in-javascript.js | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index ede0ccd..adfc97f 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -19,14 +19,29 @@ Use console.log() to clearly show the before-and-after type conversions. */ -let result = "5" - 2; +let result = Number("5") - 2; console.log("The result is: " + result); +//Converted result from String to Number -let isValid = Boolean("false"); +let isValid = Boolean(false); if (isValid) { console.log("This is valid!"); } +//Removed quotation marks to make isValid falsy rather than truthy let age = "25"; -let totalAge = age + 5; +let totalAge = Number(age) + 5; console.log("Total Age: " + totalAge); +//Converted age from String to Number to make age evaluate as a Number + +//My own example of implicit type conversion +let implicitTypeConversion +if (!implicitTypeConversion) { + console.log("This is invalid!"); +} + +//My own example of explicit type conversion +let explicitTypeConversion +if (!Boolean(explicitTypeConversion)) { + console.log("This is invalid!"); +} \ No newline at end of file