Skip to content

Commit 306c94a

Browse files
[null 병합 연산자] 번역
1 parent 2b3d061 commit 306c94a

File tree

1 file changed

+42
-42
lines changed
  • 1-js/02-first-steps/12-nullish-coalescing-operator

1 file changed

+42
-42
lines changed
Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
# Nullish coalescing operator '??'
1+
# null 병합 연산자 '??'
22

33
[recent browser="new"]
44

5-
The nullish coalescing operator `??` provides a short syntax for selecting a first "defined" variable from the list.
5+
null 병합 연산자(nullish coalescing operator) `??`를 사용하면 여러 피연산자 중 그 값이 '확정되어있는' 변수를 짧은 문법으로도 찾을 수 있습니다.
66

7-
The result of `a ?? b` is:
8-
- `a` if it's not `null` or `undefined`,
9-
- `b`, otherwise.
7+
상황에 따른 `a ?? b`의 평가 결과를 살펴봅시다.
8+
- `a``null`이나 `undefined`가 아니면 `a`
9+
- `a``null`이나 `undefined`이면 `b`
1010

11-
So, `x = a ?? b` is a short equivalent to:
11+
null 병합 연산자 `??`를 사용하지 않고 만든 `x = a ?? b`와 동일하게 동작하는 코드는 다음과 같습니다.
1212

1313
```js
1414
x = (a !== null && a !== undefined) ? a : b;
1515
```
1616

17-
Here's a longer example.
17+
길이가 길어지네요.
1818

19-
Let's say, we have a `firstName`, `lastName` or `nickName`, all of them optional.
19+
또 다른 예시를 살펴봅시다. `firstName`, `lastName`, `nickName`이란 변수가 있는데 이 값들은 모두 옵션 값이라고 해보겠습니다.
2020

21-
Let's choose the defined one and show it (or "Anonymous" if nothing is set):
21+
실제 값이 들어있는 변수를 찾고 그 값을 보여줘 보겠습니다(모든 변수에 값이 없다면 `익명`을 출력).
2222

2323
```js run
2424
let firstName = null;
2525
let lastName = null;
26-
let nickName = "Supercoder";
26+
let nickName = "바이올렛";
2727

28-
// show the first not-null/undefined variable
29-
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
28+
// null이나 undefined가 아닌 첫 번째 피연산자
29+
alert(firstName ?? lastName ?? nickName ?? "익명"); // 바이올렛
3030
```
3131
32-
## Comparison with ||
32+
## '??'와 '||'의 차이
3333
34-
That's very similar to OR `||` operator. Actually, we can replace `??` with `||` in the code above and get the same result.
34+
null 병합 연산자는 OR 연산자 `||`와 상당히 유사해 보입니다. 실제로 위 예시에서 `??``||`로 바꿔도 그 결과는 동일하기까지 하죠.
3535
36-
The important difference is that:
37-
- `||` returns the first *truthy* value.
38-
- `??` returns the first *defined* value.
36+
그런데 두 연산자 사이에는 중요한 차이점이 있습니다.
37+
- `||`는 첫 번째 *truthy* 값을 반환합니다.
38+
- `??`는 *값이 정의되어있는* 첫 번째 값을 반환합니다.
3939
40-
This matters a lot when we'd like to treat `null/undefined` differently from `0`.
40+
`null``undefined`, 숫자 `0`을 구분 지어 다뤄야 할 때 이 차이점은 매우 중요한 역할을 합니다.
4141
42-
For example:
42+
예시:
4343
4444
```js
4545
height = height ?? 100;
4646
```
4747
48-
This sets `height` to `100` if it's not defined. But if `height` is `0`, then it remains "as is".
48+
`height`에 값을 정의하지 않은 상태라면 `height``100`이 할당됩니다. 그런데 `height``0`이 할당된 상태라면 값이 바뀌지 않고 그대로 남아있습니다.
4949
50-
Let's compare it with `||`:
50+
이제 `??``||`을 비교해봅시다.
5151
5252
```js run
5353
let height = 0;
@@ -56,62 +56,62 @@ alert(height || 100); // 100
5656
alert(height ?? 100); // 0
5757
```
5858
59-
Here, `height || 100` treats zero height as unset, same as `null`, `undefined` or any other falsy value, depeding on use cases that may be incorrect.
59+
`height || 100``height``0`을 할당했지만 `0`을 falsy 한 값으로 취급했기 때문에 `null`이나 `undefined`를 할당한 것과 동일하게 처리합니다. 높이에 `0`을 할당하는 것과 유사한 유스케이스에선 이처럼 `||`는 부정확한 결과를 일으킬 수 있습니다.
6060
61-
The `height ?? 100` returns `100` only if `height` is exactly `null` or `undefined`.
61+
대신 `height ?? 100``height`가 정확히 `null`이나 `undefined`일때만 `height``100`을 할당합니다.
6262
63-
## Precedence
63+
## 연산자 우선순위
6464
65-
The precedence of the `??` operator is rather low: `7` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
65+
[`??`의 연산자 우선순위](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table)`7`로 꽤 낮습니다.
6666
67-
That's lower than most operators and a bit higher than `=` and `?`.
67+
대부분의 연산자보다 우선순위가 낮고, `=``?` 보다는 조금 높죠.
6868
69-
So if we need to use `??` in a complex expression, then consider adding parentheses:
69+
따라서 복잡한 표현식 안에서 `??`를 사용할 땐 괄호를 추가해주는 게 좋습니다.
7070
7171
```js run
7272
let height = null;
7373
let width = null;
7474

75-
// important: use parentheses
75+
// 괄호를 추가!
7676
let area = (height ?? 100) * (width ?? 50);
7777

7878
alert(area); // 5000
7979
```
8080
81-
Otherwise, if we omit parentheses, then `*` has the higher precedence and would run first. That would be the same as:
81+
`*``??`보다 우선순위가 높기 때문에 괄호를 생략하게 되면 `*`가 먼저 실행되어 아래 코드처럼 동작할 겁니다.
8282
8383
```js
84-
// not correct
84+
// 원치 않는 결과
8585
let area = height ?? (100 * width) ?? 50;
8686
```
8787
88-
There's also a related language-level limitation. Due to safety reasons, it's forbidden to use `??` together with `&&` and `||` operators.
88+
`??`엔 자바스크립트 언어에서 규정한 또 다른 제약사항이 있습니다. 안정성 관련 이슈 때문에 `??``&&``||`와 함께 사용하는 것이 금지되어 있습니다.
8989
90-
The code below triggers a syntax error:
90+
아래 예시를 실행해봅시다. 문법 에러가 발생합니다.
9191
9292
```js run
93-
let x = 1 && 2 ?? 3; // Syntax error
93+
let x = 1 && 2 ?? 3; // SyntaxError: Unexpected token '??'
9494
```
9595
96-
The limitation is surely debatable, but for some reason it was added to the language specification.
96+
이 제약에 대해선 아직 논쟁이 많긴 하지만 어쨌든 이슈가 있어서 명세서에 제약이 추가된 상황입니다.
9797
98-
Use explicit parentheses to fix it:
98+
에러를 피하려면 괄호를 사용해주세요.
9999
100100
```js run
101-
let x = (1 && 2) ?? 3; // Works
101+
let x = (1 && 2) ?? 3; // 제대로 동작합니다.
102102
alert(x); // 2
103103
```
104104
105-
## Summary
105+
## 요약
106106
107-
- The nullish coalescing operator `??` provides a short way to choose a "defined" value from the list.
107+
- null 병합 연산자 `??`를 사용하면 피연산자 중 '값이 할당된' 변수를 빠르게 찾을 수 있습니다.
108108
109-
It's used to assign default values to variables:
109+
`??`는 변수에 기본값을 할당하는 용도로 사용할 수 있습니다.
110110
111111
```js
112-
// set height=100, if height is null or undefined
112+
// height가 null이나 undefined인 경우, 100을 할당
113113
height = height ?? 100;
114114
```
115115
116-
- The operator `??` has a very low precedence, a bit higher than `?` and `=`.
117-
- It's forbidden to use it with `||` or `&&` without explicit parentheses.
116+
- `??`의 연산자 우선순위는 대다수의 연산자보다 낮고 `?``=` 보다는 높습니다.
117+
- 괄호 없이 `??``||``&&`와 함께 사용하는 것은 금지되어있습니다.

0 commit comments

Comments
 (0)