-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coercion.js
62 lines (53 loc) · 1.38 KB
/
Coercion.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
// What is Coercion ?
// Dynamically typed language used Coercion for Typecasting...
/*
Implicit Coercion
1. String + Number => String // Anything with string returns String...
2. Number + String => String
3. String + Boolean => String
4. Number + null => Number
5. Number + Undefined => Number
6. Number + Boolean => Number
7. Undefined + null => Number
8. null + Undefined => Number
9. Array + Array => String
*/
var a = 10;
undefined
var name = "ram";
undefined
name + a;
'ram10'
10 + null;
10
10 + undefined;
NaN
typeof (10 + undefined)
'number'
undefined + null;
NaN
10 + true;
11
"Amit" + true;
'Amittrue'
[] + []; Array + Array => String
''
// Explicit Coercion...
100 + parseInt("100"); // We have converted it explicitly.. parseInt() is a function
200
100 - "100" // this will work fine implicitly..
0
100 * "10" // this will work fine implicitly..
1000
100 + parseFloat("100.20"); // We have converted it explicitly.. parseFloat() is a function
200.2
"Amit" + String(2000); // We have converted it explicitly..
'Amit2000'
typeof String; String is a Function...
'function'
typeof parseInt;
'function'
var e = 100;
undefined
e.toString(); // another ques aries that, How we applied . on primitive type, we can use it only with refernce types..
'100' // Is everything Object in JS ??