-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplicit-and-implicit-conversion-in-javascript.js
More file actions
66 lines (53 loc) · 2.58 KB
/
explicit-and-implicit-conversion-in-javascript.js
File metadata and controls
66 lines (53 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
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.
*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.
*/
// PART 1: Debugged code
// Bug fix 1: "5" - 2 was actually working (subtraction coerces to number),
// but the printed message was misleading. We make the conversion explicit
// for clarity.
let result = Number("5") - 2;
console.log("The result is: " + result); // 3
// Bug fix 2: Boolean("false") returns TRUE because any non-empty string is
// truthy in JavaScript. To check whether the string equals "true", we
// compare strings directly or convert to a real boolean intent.
let rawValue = "false";
let isValid = rawValue === "true"; // Now correctly false
if (isValid) {
console.log("This is valid!");
} else {
console.log("This is not valid."); // This is what now prints.
}
// Bug fix 3: age was a string, and "+" with a string concatenates instead
// of adding. We convert age to a number with Number() before doing math.
let age = "25";
let totalAge = Number(age) + 5;
console.log("Total Age: " + totalAge); // 30
// PART 2: My own examples of implicit and explicit conversion
// Implicit conversion example with an edge case (NaN)
console.log("\n--- Implicit conversion ---");
let mysteryValue = "abc" * 2;
console.log("'abc' * 2 =", mysteryValue); // NaN
console.log("typeof mysteryValue:", typeof mysteryValue); // "number"
// Multiplying a non-numeric string forces conversion to a Number, but the
// string can't be parsed, so JavaScript returns NaN (still typed as number).
// Explicit conversion example with an edge case (null)
console.log("\n--- Explicit conversion ---");
let nothing = null;
console.log("Before:", nothing, "->", typeof nothing); // null -> object
let asNumber = Number(nothing);
console.log("After Number(null):", asNumber, "->", typeof asNumber); // 0 -> number
// null explicitly converts to 0 with Number(), even though it's "the
// absence of a value." This is one of the trickier coercions in JS:
// Number(undefined) returns NaN, but Number(null) returns 0.