From 47638c6bee7c6e4202add9ee91c6bf7cf0bd705f Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 2 Mar 2025 15:32:38 -0600 Subject: [PATCH] a commit --- ...t-and-implicit-conversion-in-javascript.js | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index ede0ccd..0e6df24 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -18,15 +18,54 @@ Use console.log() to clearly show the before-and-after type conversions. */ - -let result = "5" - 2; +// broke down the variable subtraction into more steps, making the code +// easier to follow. can convert x value with Number(), though the interpreter +// makes the decision to convert data type automatically to a number +let x = "5"; +let y = 2; +let result = x - y; console.log("The result is: " + result); -let isValid = Boolean("false"); +// Boolean() wrapper was unnecessary and caused confusion, because it wrapped a non-empty string +// which evaluated the code to boolean true. so I just initialized the false value as a bool +let isValid = false; if (isValid) { console.log("This is valid!"); } +// converting age to a primitive Number makes the addition work fine let age = "25"; -let totalAge = age + 5; +let totalAge = Number(age) + 5; console.log("Total Age: " + totalAge); + + +// MY EXAMPLES + +// EXPLICIT CONVERSION +let someInput = '5'; +let anotherInput = null; + +let sumInputs; + +if (someInput === null && anotherInput == null) { + sumInputs = 0; +} else if (someInput === null) { + sumInputs = Number(anotherInput); +} else if (anotherInput === null) { + sumInputs = Number(someInput); +} else { + sumInputs = Number(someInput) + Number(anotherInput); +} + +console.log(sumInputs); + + +// IMPLICIT CONVERSION + +let numX = 3; +let numY = '8'; +let product = numX * numY; + +console.log(product); + +