You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/08-operators/3-primitive-conversions-questions/solution.md
+4-14Lines changed: 4 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,20 +16,10 @@ undefined + 1 = NaN // (6)
16
16
"\t\n"-2=-2// (7)
17
17
```
18
18
19
-
<<<<<<< HEAD
20
-
1. 피 연산자 중 하나가 문자열인 `"" + 1`에서 `1`은 문자형으로 변환됩니다. 따라서 공백과 문자열 1을 더한, `"" + 1 = "1"`과 같은 효과를 발휘하죠. 그다음 연산 `"1" + 0`에도 같은 규칙이 적용됩니다.
19
+
1. 피연산자 중 하나가 문자열인 `"" + 1`에서 `1`은 문자형으로 변환됩니다. 따라서 공백과 문자열 1을 더한, `"" + 1 = "1"`과 같은 효과를 발휘하죠. 그다음 연산 `"1" + 0`에도 같은 규칙이 적용됩니다.
21
20
2. 뺄셈 연산자 `-`는 기타 수학 연산자처럼 숫자형만을 인수로 받습니다. 빈 문자열 `""`는 숫자 `0`으로 변환되기 때문에 결과는 `-1`이 됩니다.
22
-
3.피 연산자 중 하나가 문자열이므로 숫자 5가 문자열로 변환됩니다.
21
+
3.피연산자 중 하나가 문자열이므로 숫자 5가 문자열로 변환됩니다.
23
22
4. 뺄셈 연산자는 인수를 숫자형으로 변화시키므로 `" -9 "`는 숫자 `-9`로 변합니다. 앞, 뒤 공백은 제거되죠.
24
23
5. 숫자형으로 변환 시 `null`은 `0`이 됩니다.
25
-
6.`undefined`는 숫자형으로 변환시 `NaN`이 됩니다.
26
-
7. 문자열이 숫자형으로 변할 땐 문자열 앞뒤의 공백이 삭제됩니다. 뺄셈 연산자 앞의 피연산자는 공백을 만드는 문자 `\t`와 `\n`, 그 사이의 "일반적인" 공백으로 구성됩니다. 따라서 `" \t \n"`는 숫자형으로 변환 시 길이가 `0`인 문자열로 취급되어 숫자 `0`이 됩니다.
27
-
=======
28
-
1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied.
29
-
2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
30
-
3. The addition with a string appends the number `5` to the string.
31
-
4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it).
32
-
5.`null` becomes `0` after the numeric conversion.
33
-
6.`undefined` becomes `NaN` after the numeric conversion.
34
-
7. Space characters are trimmed off string start and end when a string is converted to a number. Here the whole string consists of space characters, such as `\t`, `\n` and a "regular" space between them. So, similarly to an empty string, it becomes `0`.
35
-
>>>>>>> upstream/master
24
+
6.`undefined`는 숫자형으로 변환 시 `NaN`이 됩니다.
25
+
7. 문자열이 숫자형으로 변할 땐 문자열 앞뒤의 공백이 삭제됩니다. 뺄셈 연산자 앞의 피연산자는 공백을 만드는 문자 `\t`와 `\n`, 그 사이의 "일반적인" 공백으로 구성됩니다. 따라서 `" \t \n"`는 숫자형으로 변환 시 길이가 `0`인 문자열로 취급되어 숫자 `0`이 됩니다.
이제 학교에서 배운 기본 산술 연산자를 넘어, 자바스크립트가 제공하는 특별한 연산자 기능에 대해 살펴봅시다.
104
-
=======
105
-
Let's meet the features of JavaScript operators that are beyond school arithmetics.
106
-
>>>>>>> upstream/master
107
85
108
86
덧셈 연산자 `+`는 대개 숫자를 더한 결과를 반환합니다.
109
87
@@ -131,16 +109,12 @@ alert( 2 + '1' ); // "21"
131
109
alert(2 + 2 + '1' ); // '221'이 아니라 '41'이 출력됩니다.
132
110
```
133
111
134
-
<<<<<<< HEAD
135
-
연산은 왼쪽에서 오른쪽으로 순차적으로 진행되기 때문에 이런 결과가 나왔습니다. 두 개의 숫자 뒤에 문자열이 오는 경우, 숫자가 먼저 더해지고, 그 후 더해진 숫자와 문자열과의 병합이 일어납니다.
136
-
=======
137
-
Here, operators work one after another. The first `+` sums two numbers, so it returns `4`, then the next `+` adds the string `1` to it, so it's like `4+'1'='41'`.
112
+
여기서는 연산자가 하나씩 순서대로 동작합니다. 첫 번째 `+`는 두 숫자를 더하므로 `4`를 반환하고, 그다음 `+`는 여기에 문자열 `'1'`을 더하므로 `4 + '1' = '41'`과 같은 결과가 됩니다.
138
113
139
114
```js run
140
-
alert('1'+2+2); // "122" and not "14"
115
+
alert('1' + 2 + 2); // "14"가 아니라 "122"가 출력됩니다.
141
116
```
142
-
Here, the first operand is a string, the compiler treats the other two operands as strings too. The `2` gets concatenated to `'1'`, so it's like `'1'+2="12"` and `"12"+2="122"`.
143
-
>>>>>>> upstream/master
117
+
첫 번째 피연산자가 문자열이므로, 컴파일러는 나머지 두 피연산자도 문자열로 취급합니다. 2가 '1'에 병합되므로, '1'+2="12"가 되고 "12"+2="122"가 됩니다.
144
118
145
119
이처럼 이항 덧셈 연산자 `+`는 문자열 연결과 변환이라는 특별한 기능을 제공합니다. 다른 산술 연산자가 오직 숫자형의 피연산자만 다루고, 피연산자가 숫자형이 아닌 경우에 그 형을 숫자형으로 바꾸는 것과는 대조적입니다.
자바스크립트는 다양한 연산자를 제공하는데, 이 모든 연산자엔 우선순위가 매겨져 있습니다. 우선순위 숫자가 클수록 먼저 실행됩니다. 순위가 같으면 왼쪽부터 시작해서 오른쪽으로 연산이 수행됩니다.
218
192
219
-
<<<<<<< HEAD
220
193
아래는 [우선순위 테이블(precedence table)](https://developer.mozilla.org/en/JavaScript/Reference/operators/operator_precedence)의 일부를 발췌한 표입니다. 순서를 기억할 필요는 없지만, 동일한 기호의 단항 연산자는 이항 연산자보다 우선순위가 더 높다는 것에 주목해 주시기 바랍니다.
221
194
222
195
| 순위 | 연산자 이름 | 기호 |
223
196
|---|------|---|
224
197
|...|...|...|
225
-
| 17 | 단항 덧셈 | `+` |
226
-
| 17 | 단항 부정 | `-` |
227
-
| 16 | 지수 | `**` |
228
-
| 15 | 곱셈 | `*` |
229
-
| 15 | 나눗셈 | `/` |
230
-
| 13 | 덧셈 | `+` |
231
-
| 13 | 뺄셈 | `-` |
198
+
|14| 단항 덧셈 |`+`|
199
+
|14| 단항 부정 |`-`|
200
+
|13|거듭제곱|`**`|
201
+
|12| 곱셈 |`*`|
202
+
|12| 나눗셈 |`/`|
203
+
|11| 덧셈 |`+`|
204
+
|11| 뺄셈 |`-`|
232
205
|...|...|...|
233
-
| 3 | 할당 | `=` |
206
+
|2| 할당 |`=`|
234
207
|...|...|...|
235
208
236
-
'단항 덧셈 연산자'는 우선순위 `17`로, '(이항) 덧셈 연산자'의 우선순위 `13`보다 높습니다. 표현식 `"+apples + +oranges"`에서 단항 덧셈 연산자가 덧셈보다 먼저 수행되는 이유가 바로 이 때문입니다.
237
-
=======
238
-
Here's an extract from the [precedence table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence) (you don't need to remember this, but note that unary operators are higher than corresponding binary ones):
239
-
240
-
| Precedence | Name | Sign |
241
-
|------------|------|------|
242
-
| ... | ... | ... |
243
-
| 14 | unary plus | `+` |
244
-
| 14 | unary negation | `-` |
245
-
| 13 | exponentiation | `**` |
246
-
| 12 | multiplication | `*` |
247
-
| 12 | division | `/` |
248
-
| 11 | addition | `+` |
249
-
| 11 | subtraction | `-` |
250
-
| ... | ... | ... |
251
-
| 2 | assignment | `=` |
252
-
| ... | ... | ... |
253
-
254
-
As we can see, the "unary plus" has a priority of `14` which is higher than the `11` of "addition" (binary plus). That's why, in the expression `"+apples + +oranges"`, unary pluses work before the addition.
255
-
>>>>>>> upstream/master
209
+
'단항 덧셈 연산자'는 우선순위 `14`로, '(이항) 덧셈 연산자'의 우선순위 `11`보다 높습니다. 표현식 `"+apples + +oranges"`에서 단항 덧셈 연산자가 덧셈보다 먼저 수행되는 이유가 바로 이 때문입니다.
256
210
257
211
## 할당 연산자
258
212
259
-
<<<<<<< HEAD
260
-
무언가를 할당할 때 쓰이는 `=`도 연산자입니다. 이 연산자는 할당(assignment) 연산자라고 불리는데, 우선순위는 `3`으로 아주 낮습니다.
261
-
=======
262
-
Let's note that an assignment `=` is also an operator. It is listed in the precedence table with the very low priority of `2`.
263
-
>>>>>>> upstream/master
213
+
무언가를 할당할 때 쓰이는 `=`도 연산자입니다. 이 연산자는 할당(assignment) 연산자라고 불리는데, 우선순위는 `2`으로 아주 낮습니다.
264
214
265
215
`x = 2 * 2 + 1`과 같은 표현식에서 계산이 먼저 이뤄지고, 그 결과가 `x`에 할당되는 이유가 바로 이 때문입니다.
266
216
@@ -274,11 +224,7 @@ alert( x ); // 5
274
224
275
225
`=`는 연산자이기 때문에 흥미로운 함축성을 내포하고 있습니다.
276
226
277
-
<<<<<<< HEAD
278
-
자바스크립트에서 대부분의 연산자들은 값을 반환합니다. `+`와 `-`뿐만 아니라 `=` 역시 값을 반환하죠.
279
-
=======
280
-
All operators in JavaScript return a value. That's obvious for `+` and `-`, but also true for `=`.
281
-
>>>>>>> upstream/master
227
+
자바스크립트의 모든 연산자는 값을 반환합니다. `+`와 `-`뿐만 아니라 `=` 역시 값을 반환하죠.
282
228
283
229
`x = value`을 호출하면 `value`가 `x`에 쓰여지고, 이에 더하여 *`value`가 반환됩니다*.
284
230
@@ -320,11 +266,7 @@ alert( c ); // 4
320
266
321
267
이렇게 할당 연산자를 여러 개 연결한 경우, 평가는 우측부터 진행됩니다. 먼저 가장 우측의 `2 + 2`가 평가되고, 그 결과가 좌측의 `c`, `b`, `a`에 순차적으로 할당됩니다. 모든 변수가 단일 값을 공유하게 되죠.
322
268
323
-
<<<<<<< HEAD
324
269
그런데 되도록이면 연산자를 체이닝 하는것 보다 가독성을 위해 아래와 같이 줄을 나눠 코드를 작성하길 권유드립니다.
325
-
=======
326
-
Once again, for the purposes of readability it's better to split such code into a few lines:
327
-
>>>>>>> upstream/master
328
270
329
271
```js
330
272
c = 2 + 2;
@@ -364,11 +306,7 @@ let n = 2;
364
306
365
307
n *= 3 + 5; // right part evaluated first, same as n *= 8
366
308
367
-
<<<<<<<HEAD
368
-
alert( n ); // 16 (*=의 우측이 먼저 평가되므로, 위 식은 n *= 8과 동일합니다.)
369
-
=======
370
309
alert( n ); // 16
371
-
>>>>>>> upstream/master
372
310
```
373
311
374
312
## 증가·감소 연산자
@@ -500,11 +438,7 @@ counter++;
500
438
- 오른쪽 시프트(RIGHT SHIFT) ( `>>` )
501
439
- 부호 없는 오른쪽 시프트(ZERO-FILL RIGHT SHIFT) ( `>>>` )
502
440
503
-
<<<<<<< HEAD
504
-
비트 연산자는 저수준(2진 표현)에서 숫자를 다뤄야 할 때 쓰이므로 흔하게 쓰이진 않습니다. 웹 개발 시엔 이런 일이 자주 일어나지 않기 때문에 비트 연산자를 만날 일은 거의 없죠. 그렇긴 해도 암호를 다뤄야 할 땐 비트 연산자가 유용하기 때문에 때가 되면 MDN의 [비트 연산자](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) 문서를 보시는 걸 추천합니다.
505
-
=======
506
-
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#bitwise_operators) chapter on MDN when a need arises.
507
-
>>>>>>> upstream/master
441
+
비트 연산자는 저수준(2진 표현)에서 숫자를 다뤄야 할 때 쓰이므로 흔하게 쓰이진 않습니다. 웹 개발 시엔 이런 일이 자주 일어나지 않기 때문에 비트 연산자를 만날 일은 거의 없죠. 그렇긴 해도 암호를 다뤄야 할 땐 비트 연산자가 유용하기 때문에 때가 되면 MDN의 [비트 연산자](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#bitwise_operators) 문서를 보시는 걸 추천합니다.
0 commit comments