Skip to content

Commit 74307d2

Browse files
committed
The updated assignment file.
1 parent ce92942 commit 74307d2

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

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

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

2121

22-
let result = "5" - 2;
22+
let result = "5" - 2; // The string "5" is implicitly converted to a number 5 due to the subtraction operator. If the addition operator was used, the string "5" would be concatenated to the number 2 to get the string "52".
2323
console.log("The result is: " + result);
2424

25-
let isValid = Boolean("false");
25+
let isValid = Boolean("false"); // The string "false" is a truthy value, so the boolean function will return true. I am unsure if this code is supposed to have an error, or if it is supposed to run due to a true boolean value.
2626
if (isValid) {
2727
console.log("This is valid!");
2828
}
2929

30-
let age = "25";
30+
let age = Number("25"); // Explicit type conversion from string to number. By doing this, the string "25" is converted to a number 25 which can now be added to 5 to get the total age.
3131
let totalAge = age + 5;
3232
console.log("Total Age: " + totalAge);
33+
34+
// Part 2: Write Your Own Examples
35+
// Implicit Type Conversion Example
36+
let implicitConversion = "5" + 2;
37+
console.log(implicitConversion);
38+
39+
// Explicit Type Conversion Example
40+
let explicitConversion = Number("5") + 2;
41+
console.log(explicitConversion);
42+
43+
//edge case example
44+
let emptyNumber; //This variable is undefined.
45+
console.log(emptyNumber);
46+
emptyNumber = Boolean(emptyNumber); //This variable is now a boolean value of false.
47+
console.log(emptyNumber);
48+
let edgeCase = Number("5") + Number(emptyNumber); //The string "5" is converted to a number 5, and the boolean value of false is converted to a number 0.
49+
console.log(Number(emptyNumber));
50+
console.log(edgeCase);

0 commit comments

Comments
 (0)