-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path02_type_conversion.js
99 lines (90 loc) · 4.33 KB
/
02_type_conversion.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* The `typeConversion` function demonstrates various techniques of type conversion in JavaScript.
* This includes both explicit and implicit type conversions, each of which is outlined below.
*/
function typeConversion() {
/**
* =================================
* String to Number
* =================================
* You can convert a string to a number using the unary '+' operator or the Number() function.
* Both methods return NaN (Not-a-Number) if the string cannot be converted to a number.
*/
let strToNum = +"42";
let strToNumUsingFunc = Number("42");
console.log(`String to Number using unary '+': ${strToNum}, typeof: ${typeof strToNum}`);
console.log(`String to Number using Number(): ${strToNumUsingFunc}, typeof: ${typeof strToNumUsingFunc}`);
/**
* =================================
* Number to String
* =================================
* Numbers can be converted to strings using the String() function or the .toString() method.
*/
let numToStr = String(42);
let numToStrUsingMethod = (42).toString();
console.log(`Number to String using String(): ${numToStr}, typeof: ${typeof numToStr}`);
console.log(`Number to String using .toString(): ${numToStrUsingMethod}, typeof: ${typeof numToStrUsingMethod}`);
/**
* =================================
* Boolean to Number
* =================================
* Boolean values can be converted to numbers using the Number() function.
* Here, 'true' is converted to 1 and 'false' would be converted to 0.
*/
let boolToNum = Number(true);
console.log(`Boolean to Number: ${boolToNum}, typeof: ${typeof boolToNum}`);
/**
* =================================
* Number to Boolean
* =================================
* Numbers can be converted to boolean values using the Boolean() function.
* Here, any non-zero number will be converted to 'true' and zero to 'false'.
*/
let numToBool = Boolean(1);
console.log(`Number to Boolean: ${numToBool}, typeof: ${typeof numToBool}`);
/**
* =================================
* Implicit Conversion
* =================================
* JavaScript performs implicit type conversion in certain expressions.
* For example, using '+' with a string and a number results in string concatenation.
*/
let implicit = "5" + 1;
let implicitTwo = "5" * 1;
console.log(`Implicit Conversion (String + Number): ${implicit}, typeof: ${typeof implicit}`);
console.log(`Implicit Conversion (String * Number): ${implicitTwo}, typeof: ${typeof implicitTwo}`);
/**
* =================================
* ParseInt and ParseFloat
* =================================
* The parseInt() and parseFloat() functions can convert a string into an integer or a floating-point number.
* The second argument of parseInt is the radix, which specifies the base of the numeral system.
*/
let parsedInt = parseInt("42px", 10);
let parsedFloat = parseFloat("42.42px");
console.log(`parseInt: ${parsedInt}, typeof: ${typeof parsedInt}`);
console.log(`parseFloat: ${parsedFloat}, typeof: ${typeof parsedFloat}`);
/**
* =================================
* JSON.parse and JSON.stringify
* =================================
* These functions are used for converting objects and arrays to JSON-formatted strings, and vice versa.
*/
let obj = { key: "value" };
let objToStr = JSON.stringify(obj);
let strToObj = JSON.parse(objToStr);
console.log(`JSON.stringify: ${objToStr}, typeof: ${typeof objToStr}`);
console.log(`JSON.parse: ${JSON.stringify(strToObj)}, typeof: ${typeof strToObj}`);
/**
* =================================
* Nuances and Special Cases
* =================================
* 1) Using '+' with a string and a number results in string concatenation.
* 2) Despite representing an invalid or unrepresentable value, typeof NaN returns 'number'.
* 3) Using parseInt() without specifying a radix can yield unpredictable results, especially with older browsers.
*/
console.log(`Nuances: '5' + 1 results in a string: ${"5" + 1}`);
console.log(`Nuances: typeof NaN is ${typeof NaN}`);
console.log(`Nuances: parseInt('010') without radix is ${parseInt("010")}`);
}
typeConversion();