-
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
35 lines (28 loc) · 1.37 KB
/
explicit-and-implicit-conversion-in-javascript.js
File metadata and controls
35 lines (28 loc) · 1.37 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
// Explicitly convert "5" to a number to make the subtraction clear and intentional
let result = Number("5") - 2;
console.log("The result is: " + result); // Output: The result is: 3
// "false" (as a string) is truthy, so Boolean("false") returns true. This can be confusing.
// To handle this correctly, check for actual boolean or use string comparison if needed
let isValid = ("false" === "true"); // This will be false
if (isValid) {
console.log("This is valid!");
} else {
console.log("This is NOT valid."); // Correct output
}
// Convert age to a number before performing addition
let age = "25";
let totalAge = Number(age) + 5;
console.log("Total Age: " + totalAge); // Output: Total Age: 30
//Implicit type conversion
let implicitResult = "10" * 2; // JavaScript automatically converts "10" to number
console.log("Implicit conversion ('10' * 2):", implicitResult); // 20
console.log("Type of result:", typeof implicitResult); // number
//Edgecase NaN
let badNumber = "abc" * 2; // "abc" can't be converted to a number
console.log("Implicit conversion ('abc' * 2):", badNumber); // NaN
console.log("Type of result:", typeof badNumber); // number (but it's NaN)
//Explicit Type Conversion
let value = null;
let explicitNumber = Number(value);
console.log("Explicit conversion Number(null):", explicitNumber); // 0
console.log("Type of result:", typeof explicitNumber); // number