Skip to content

Commit 2621187

Browse files
committed
Fixed bugs and added my own examples
1 parent ce92942 commit 2621187

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

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

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

2121

22-
let result = "5" - 2;
22+
let result = Number("5") - 2; //added explicit Number conversion
2323
console.log("The result is: " + result);
2424

25-
let isValid = Boolean("false");
25+
let isValid = Boolean(false);//removed quotes so that it will be seen as a false statement.
2626
if (isValid) {
2727
console.log("This is valid!");
28+
} else { //added else statement so it can still run
29+
console.log("This is not valid!");
2830
}
2931

3032
let age = "25";
31-
let totalAge = age + 5;
33+
let totalAge = Number(age) + 5; // was outputting 52 due to implicit conversion. Add Number conversion to fix output.
3234
console.log("Total Age: " + totalAge);
35+
36+
//my own examples
37+
38+
//explicit type conversion:
39+
let numberOfApples = Number("10");//I used the Number conversion to explicitly make this a number despite it being in quotes making it a string
40+
let numberOfOranges = 20;
41+
let sumFruit = numberOfApples + numberOfOranges;
42+
console.log("The fantastical fruit total is " + sumFruit);
43+
44+
//implicit type conversion:
45+
46+
let sum = 30 + "2";
47+
console.log(sum);//returns 302 because 30 was implicitly converted to a string and added to the string "2".

0 commit comments

Comments
 (0)