-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathexplicit-and-implicit-conversion-in-javascript.js
More file actions
37 lines (27 loc) · 1.21 KB
/
explicit-and-implicit-conversion-in-javascript.js
File metadata and controls
37 lines (27 loc) · 1.21 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
/*
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.
*/
let result = "5" - 2;//converts "5" into number automatically
console.log("The result is: " + result);// 3
let isValid = Boolean("false");//false is truthy
if (isValid) {
console.log("This is valid!");// true
}
let age = "25";
let totalAge = Number(age) + 5;//Number() function is used to convert string into number
console.log("Total Age: " + totalAge);// 30
console.log("5" * "2");// 10 - Example for Implicit type conversion
let num = null;
console.log(Boolean(num));// false - Example for Explicit type conversion