forked from suhailroushan13/365days.codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.js
263 lines (166 loc) · 6.07 KB
/
23.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Type Conversions
// In programming, type conversion is the process of converting data of one type to another. For example: converting String data to Number.
// There are two types of type conversion in JavaScript.
// Implicit Conversion - automatic type conversion
// Explicit Conversion - manual type conversion
// JavaScript Implicit Conversion
// In certain situations, JavaScript automatically converts one data type to another (to the right type). This is known as implicit conversion.
// Example 1: Implicit Conversion to String
// numeric string used with + gives string type
let result;
result = '3' + 2;
console.log(result) // "32"
result = '3' + true;
console.log(result); // "3true"
result = '3' + undefined;
console.log(result); // "3undefined"
result = '3' + null;
console.log(result); // "3null"
// Example 2: Implicit Conversion to Number
// numeric string used with - , / , * results number type
let result1;
result1 = '4' - '2';
console.log(result1); // 2
result1 = '4' - 2;
console.log(result1); // 2
result1 = '4' * 2;
console.log(result1); // 8
result1 = '4' / 2;
console.log(result1); // 2
// Example 3: Non-numeric String Results to NaN
// non-numeric string used with - , / , * results to NaN
let result2;
result2 = 'hello' - 'world';
console.log(result2); // NaN
result2 = '4' - 'hello';
console.log(result2); // NaN
// Example 4: Implicit Boolean Conversion to Number
// if boolean is used, true is 1, false is 0
let result3;
result3 = '4' - true;
console.log(result3); // 3
result3 = 4 + true;
console.log(result3); // 5
result3 = 4 + false;
console.log(result3); // 4
// Example 5: null Conversion to Number
// null is 0 when used with number
let result4;
result4 = 4 + null;
console.log(result4); // 4
result4 = 4 - null;
console.log(result4); // 4
// Example 6: undefined used with number, boolean or null
// Arithmetic operation of undefined with number, boolean or null gives NaN
let result5;
result5 = 4 + undefined;
console.log(result5); // NaN
result5 = 4 - undefined;
console.log(result5); // NaN
result5 = true + undefined;
console.log(result5); // NaN
result5 = null + undefined;
console.log(result5); // NaN
// JavaScript Explicit Conversion
// You can also convert one data type to another as per your needs. The type conversion that you do manually is known as explicit type conversion.
// In JavaScript, explicit type conversions are done using built-in methods.
// Here are some common methods of explicit conversions.
// 1. Convert to Number Explicitly
// To convert numeric strings and boolean values to numbers, you can use Number(). For example,
let result6;
// string to number
result6 = Number('324');
console.log(result6); // 324
result6 = Number('324e-1')
console.log(result6); // 32.4
// boolean to number
result6 = Number(true);
console.log(result6); // 1
result6 = Number(false);
console.log(result6); // 0
// In JavaScript, empty strings and null values return 0. For example,
let result7;
result7 = Number(null);
console.log(result7); // 0
let result8 = Number(' ')
console.log(result8); // 0
// If a string is an invalid number, the result will be NaN. For example,
let result9;
result9 = Number('hello');
console.log(result9); // NaN
result9 = Number(undefined);
console.log(result9); // NaN
result9 = Number(NaN);
console.log(result9); // NaN
// Note: You can also generate numbers from strings using parseInt(), parseFloat(), unary operator + and Math.floor(). For example,
let result10;
result10 = parseInt('20.01');
console.log(result10); // 20
result10 = parseFloat('20.01');
console.log(result10); // 20.01
result10 = +'20.01';
console.log(result10); // 20.01
result10 = Math.floor('20.01');
console.log(result10); // 20
// 2. Convert to String Explicitly
// To convert other data types to strings, you can use either String() or toString(). For example,
//number to string
let result11;
result11 = String(324);
console.log(result11); // "324"
result11 = String(2 + 4);
console.log(result11); // "6"
//other data types to string
result11 = String(null);
console.log(result11); // "null"
result11 = String(undefined);
console.log(result11); // "undefined"
result11 = String(NaN);
console.log(result11); // "NaN"
result11 = String(true);
console.log(result11); // "true"
result11 = String(false);
console.log(result11); // "false"
// using toString()
result11 = (324).toString();
console.log(result11); // "324"
result11 = true.toString();
console.log(result11); // true
// Note: String() takes null and undefined and converts them to string.
// However, toString() gives error when null are passed.
// 3. Convert to Boolean Explicitly
// To convert other data types to a boolean, you can use Boolean().
//
// In JavaScript, undefined, null, 0, NaN, '' converts to false. For example,
let result12;
result12 = Boolean('');
console.log(result12); // false
result12 = Boolean(0);
console.log(result12); // false
result12 = Boolean(undefined);
console.log(result12); // false
result12 = Boolean(null);
console.log(result12); // false
result12 = Boolean(NaN);
console.log(result12); // false
// All other values give true. For example,
result12 = Boolean(324);
console.log(result); // true
result12 = Boolean('hello');
console.log(result12); // true
result12 = Boolean(' ');
console.log(result12); // true
// JavaScript Type Conversion Table
// The table shows the conversion of different values to String, Number, and Boolean in JavaScript.
// Value StringConversion Number Conversion Boolean Conversion
// 1 "1" 1 true
// 0 "0" 0 false
// "1" "1" 1 true
// "0" "0" 0 true
// "ten" "ten" NaN true
// true "true" 1 true
// false "false" 0 false
// null "null" 0 false
// undefined "undefined" NaN false
// '' "" 0 false
// ' ' " " 0 true