Skip to content

Commit 3985e0e

Browse files
Update explicit-and-implicit-conversion-in-javascript.js
1 parent ffc5e48 commit 3985e0e

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

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

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

2121

22-
let result = "5" - 2; can run either way as it is subtraction. It may look better without quotes.
23-
let result = Number("5") - 2;
24-
console.log("The result is: " + result + 2);
22+
let result = "5" - 2; // can run either way as it is subtraction- implicit converstion changes 5 into a number. It may look better without quotes.
23+
let result = Number("5") - 2; // would be an explicit conversion
2524
console.log("The result is: " + result);
2625

27-
let isValid = Boolean("false");
26+
let isValid = Boolean("false"); //this automatically runs true because it is a string, not a 0, null, undefined, etc. value- explicit conversion
2827
if (isValid) {
2928
console.log("This is valid!");
3029
}
3130

32-
let age = "25";
33-
let totalAge = age + 5;
34-
console.log("Total Age: " + totalAge);
31+
let age = "25"; // Declare a variable 'age'and assign to a string value "25"; // fixes the out come of 255
32+
let totalAge = Number(age) + 5;// convert 'age' from a string to a number then add 5
33+
console.log("Total Age: " + totalAge); explicit conversion
34+
//Implicit Conversion:
35+
let studentAge = "10" * 2;
36+
console.log("The student's age is:" , studentAge); // Output: 20
37+
// Explicit Conversion:
38+
let studentAge = Number("10") * 2;
39+
console.log("The student's age is:", studentAge); // Output:20
40+
// Explicit Conversion using Edge Case- Null
41+
let studentAge= Number(null) * 2;
42+
console.log("The student's age is:", studentAge); // Output: 0

0 commit comments

Comments
 (0)