Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit516a589

Browse files
committed
type coercion
1 parentc39727c commit516a589

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This repository will cover the following topics:
2626
16.[Memory](memory.md)
2727
17.[Engine](engine.md)
2828
18.[Runtime Environment](runtime-environment.md)
29+
19.[Type Coercion](type-coercion.md)
2930

3031
---
3132

@@ -101,4 +102,6 @@ https://blog.bitsrc.io/javascript-internals-javascript-engine-run-time-environme
101102

102103
https://blog.sessionstack.com/how-does-javascript-actually-work-part-1-b0bacc073cf
103104

104-
http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/
105+
http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/
106+
107+
https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/#:~:text=Type%20coercion%20is%20the%20process,boolean%2C%20and%20so%20on).&text=As%20an%20example%20of%20type,different%20a%20and%20b%20types.

‎type-coercion.md‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
##Type Coercion:
2+
3+
* Type coercion is the process of converting value from one type to another example: string to number, or number to string.
4+
* Since javascript is a weakly typed language type coercion can be done to values implicitly or explicitly
5+
6+
###Explicit type conversion
7+
8+
When we convert value from type to another explicitly it is called type casting example:`Number('10')` this is a way of converting value from`string` to`number` explicitly
9+
10+
###Implicit type conversion(type coercion):
11+
12+
Implicit type conversion can happen when we apply some operators on values, example:`!10` here value 10 is converted from number to boolean implicitly with type coercion because of logical operator`NOT` that we added before the number
13+
14+
---
15+
16+
###Types of conversion
17+
18+
1. to string: Explicitly using`String(value)` or implicitly using`value + ''`
19+
2. to boolean: Explicitly using`Boolean(value)` or implicitly using logical operators`(|| && !)`
20+
3. to number: Explicitly using`Number(value)` or implicitly using more operators
21+
22+
####Important note:
23+
conversion logic for primitives and objects works differently but both can be converted to any of the three types mentioned above
24+
25+
---
26+
27+
##Conversion to string:
28+
29+
We can convert any primitive type to string explicitly using`String()` and we can do it implicitly using the`+` operator with a string
30+
31+
32+
####Example 19.0:
33+
34+
Conversion to string
35+
36+
```javascript
37+
// Explicit conversion to string
38+
String(123)// '123'
39+
String(-12.3)// '-12.3'
40+
String(null)// 'null'
41+
String(undefined)// 'undefined'
42+
String(true)// 'true'
43+
String(false)// 'false'
44+
45+
// Implicit conversion to string
46+
123+''// '123'
47+
```
48+
49+
---
50+
51+
##Conversion to boolean:
52+
53+
We can convert any primitive to boolean type explicitly using`Boolean()` and we can do it implicitly using logical operators`(|| && !)`
54+
55+
####Example 19.1:
56+
57+
Conversion to boolean
58+
59+
```javascript
60+
// Explicit conversion to string
61+
Boolean(2)// true
62+
63+
// Implicit conversion to string
64+
if (2) { }// if condition will return true
65+
!2// false
66+
2||'hello'// this will return 2
67+
```
68+
69+
##Boolean implicit conversion will return operand value:
70+
71+
`||` and`&&` operators will do implicit conversion internally but it will return the operand value
72+
73+
74+
---
75+
76+
##Conversion to number:
77+
78+
We can convert any primitive to number type explicitly using`Number()` and implicitly using the following operators
79+
80+
1. Comparison operators`>``<``<=``>=`
81+
2. Bitwise operators`|``&``^``~`
82+
3. Arithmetic operators`-``+``*``/``%` the`+` will not make number conversion if any side is string
83+
4. Unary`+` operator
84+
5. Loose equality operator`==` only when both sides are not strings and they are not`null` or`undefined`
85+
86+
```javascript
87+
Number('123')// explicit
88+
+'123'// implicit
89+
123!='456'// implicit
90+
4>'5'// implicit
91+
5/null// implicit
92+
true|0// implicit
93+
```
94+
95+
---
96+
97+
##Some conversion values
98+
99+
```javascript
100+
Number(null)// 0
101+
Number(undefined)// NaN
102+
Number(true)// 1
103+
Number(false)// 0
104+
null==false// false -> because the false is coerced to 0 so the comaprison is null == 0
105+
undefined==false// false -> because the false is coerced to 0 so the comparison is undefined == 0
106+
"1"==true// true -> because the true is coerced to 1 and then the "1" is coerced to 1 so the comparison is 1 == 1
107+
"0"==false// true -> because the false is coerced to 0 and then the "0" is coerced to 0 so the comparison is 0 == 0
108+
3+"1"// "31" -> 3 will be coerced to "3"
109+
3++"1"// 4 -> the right unary + will convert "1" to 1 and it will be 3 + 1 = 4
110+
3+true// 4 -> true is coerced to 1 so it will be 3 + 1
111+
3+false// 3 -> false is coerced to 0
112+
true+false// 1 -> true is coerced to 1 and false is coerced to 0
113+
114+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp