Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions explicit-and-implicit-conversion-in-javascript.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
/*

Part 1: Debugging Challenge
The JavaScript code below contains intentional bugs related to type conversion.
Please do the following:
- Run the script to observe unexpected outputs.
- Debug and fix the errors using explicit type conversion methods like Number() , String() , or Boolean() where necessary.
- Annotate the code with comments explaining why the fix works.

Part 2: Write Your Own Examples
Write their own code that demonstrates:
- One example of implicit type conversion.
- One example of explicit type conversion.
Original Code and Unexpected Behavior

*We encourage you to:
Include at least one edge case, like NaN, undefined, or null .
Use console.log() to clearly show the before-and-after type conversions.
javascript
let result = "5" - 2;
console.log("The result is: " + result); // Outputs: 3

*/
let isValid = Boolean("false");
if (isValid) {
console.log("This is valid!"); // Outputs: This is valid! (unexpected)
}

let age = "25";
let totalAge = age + 5;
console.log("Total Age: " + totalAge); // Outputs: Total Age: 255 (unexpected)

let result = "5" - 2;
console.log("The result is: " + result);

let isValid = Boolean("false");
Fixed Code with Comments

javascript
// "5" - 2 works because JavaScript implicitly converts "5" to a number.
// No fix needed here.
let result = Number("5") - 2;
console.log("The result is: " + result); // Outputs: 3

// Boolean("false") returns true because any non-empty string is truthy.
// To fix this, we need to check the actual string value.
let isValid = ("false" === "true"); // Explicit comparison instead of Boolean conversion
if (isValid) {
console.log("This is valid!");
} else {
console.log("This is NOT valid!"); // Correct output
}

// "25" + 5 results in string concatenation: "255"
// Fix by converting "25" to a number first
let age = "25";
let totalAge = age + 5;
console.log("Total Age: " + totalAge);
let totalAge = Number(age) + 5;
console.log("Total Age: " + totalAge); // Outputs: 30


Your Own Examples

Implicit Type Conversion

javascript
let value = "10" * 2; // "10" is implicitly converted to number
console.log("Implicit conversion result:", value); // Outputs: 20

let input = undefined;
console.log("Before conversion:", input, "Type:", typeof input);

let converted = String(input); // Converts undefined to "undefined"
console.log("After conversion:", converted, "Type:", typeof converted); // Outputs: "undefined", string