You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: explicit-and-implicit-conversion-in-javascript.js
+27-4Lines changed: 27 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -19,14 +19,37 @@ Use console.log() to clearly show the before-and-after type conversions.
19
19
*/
20
20
21
21
22
-
letresult="5"-2;
22
+
letresult=Number("5")-2;//this looked correct to me and displayed how I expected. I added Number() to be more clear
23
23
console.log("The result is: "+result);
24
24
25
-
letisValid=Boolean("false");
25
+
letisValid=Boolean("false"==false);//I added '==' so that the string 'false' will be equal to the boolean false.
26
26
if(isValid){
27
27
console.log("This is valid!");
28
-
}
28
+
}
29
+
29
30
30
31
letage="25";
31
-
lettotalAge=age+5;
32
+
33
+
lettotalAge=Number(age)+5;//turned the variable 'age' from string to number so the equation can work
32
34
console.log("Total Age: "+totalAge);
35
+
36
+
37
+
//Part Two
38
+
39
+
//Implicit type conversion
40
+
letnumofItems=38;
41
+
console.log(`You have ${numofItems} items in your cart.`);//Template literal
42
+
43
+
//Explicit Type Conversion
44
+
letstr="72";
45
+
letnum=Number(str);
46
+
47
+
console.log(num);// converting to a number from string
48
+
49
+
//One edge case
50
+
51
+
letslicesofPizza=Boolean(null);//used null to for Boolean value and 'if()' to create output if returned true. If false, code shows the expected output.
0 commit comments