@@ -10,19 +10,19 @@ In case the behavior of the implicit conversion is not sure, the constructors of
10
10
11
11
The Number 10 is converted to string '10' and then '+' concatenates both strings
12
12
13
- ```
13
+ ``` javascript
14
14
var x = 10 + " 20" ;
15
15
var y = " 20" + 10 ;
16
16
```
17
17
18
18
### The Boolean value true is converted to string 'true' and then '+'concatenates both the strings
19
19
20
- ```
20
+ ``` javascript
21
21
var z = true + " 10" ;
22
22
console .log (x, y, z); // 1020 2010 true10
23
23
```
24
24
25
- ```
25
+ ``` javascript
26
26
var w = 10 - " 5" ;
27
27
var x = 10 * " 5" ;
28
28
var y = 10 / " 5" ;
@@ -33,65 +33,65 @@ console.log(w, x, y, z); //5 50 2 0
33
33
34
34
### The Boolean value true is converted to number 1 and then operation is performed
35
35
36
- ```
36
+ ``` javascript
37
37
var x = true + 2 ;
38
38
```
39
39
40
40
### The Boolean value false is converted to number 0 and then operation is performed
41
41
42
- ```
42
+ ``` javascript
43
43
var y = false + 2 ;
44
44
45
45
console .log (x, y); // 3 2
46
46
```
47
47
48
48
### Should output 'true' as string '10' is coerced to number 10
49
49
50
- ```
50
+ ``` javascript
51
51
var x = 10 == " 10" ;
52
52
```
53
53
54
54
### Should output 'true', as boolean true is coerced to number 1
55
55
56
- ```
56
+ ``` javascript
57
57
var y = true == 1 ;
58
58
```
59
59
60
60
### Should output 'false' as string 'true' is coerced to NaN which is not equal to 1 of Boolean true
61
61
62
- ```
62
+ ``` javascript
63
63
var z = true == " true" ;
64
64
65
65
console .log (x, y, z); // true true false
66
66
```
67
67
68
68
### Number to String
69
69
70
- ```
70
+ ``` javascript
71
71
let ans = String (10 );
72
72
console .log (typeof ans); // string
73
73
```
74
74
75
75
### String to Number
76
76
77
- ```
77
+ ``` javascript
78
78
ans = Number (" 10" );
79
79
console .log (ans); // number
80
80
```
81
81
82
- ```
82
+ ``` javascript
83
83
ans = Number (" learnjavascript" );
84
84
console .log (ans); // NaN
85
85
```
86
86
87
87
### Number to Boolean
88
88
89
- ```
89
+ ``` javascript
90
90
ans = Boolean (10 );
91
91
console .log (ans); // true
92
92
```
93
93
94
- ```
94
+ ``` javascript
95
95
ans = Boolean (0 );
96
96
console .log (ans); // false
97
97
```
@@ -105,19 +105,19 @@ implicit type conversion me javascrupt otomatic ak type se dusri type me convert
105
105
number se string me conversion
106
106
string se number me convrsion
107
107
108
- ```
108
+ ``` javascript
109
109
let x = " 20" ;
110
110
console .log (x / 2 );
111
111
```
112
112
113
113
### Explicit type and manual type conversion
114
114
115
- ```
115
+ ``` javascript
116
116
let x = " 20" ;
117
117
console .log (Number (x) + 20 ); // //string to number
118
118
```
119
119
120
- ```
120
+ ``` javascript
121
121
let y = 10 ;
122
122
console .log (string (y) + 200 ); // number to string
123
123
```
0 commit comments