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
That's very similar to OR `||` operator. Actually, we can replace `??` with `||` in the code above and get the same result.
34
+
null 병합 연산자는 OR 연산자 `||`와 상당히 유사해 보입니다. 실제로 위 예시에서 `??`를 `||`로 바꿔도 그 결과는 동일하기까지 하죠.
35
35
36
-
The important difference is that:
37
-
- `||` returns the first *truthy* value.
38
-
- `??` returns the first *defined* value.
36
+
그런데 두 연산자 사이에는 중요한 차이점이 있습니다.
37
+
- `||`는 첫 번째 *truthy* 값을 반환합니다.
38
+
- `??`는 *값이 정의되어있는* 첫 번째 값을 반환합니다.
39
39
40
-
This matters a lot when we'd like to treat `null/undefined` differently from `0`.
40
+
`null`과 `undefined`, 숫자 `0`을 구분 지어 다뤄야 할 때 이 차이점은 매우 중요한 역할을 합니다.
41
41
42
-
For example:
42
+
예시:
43
43
44
44
```js
45
45
height = height ??100;
46
46
```
47
47
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`이 할당된 상태라면 값이 바뀌지 않고 그대로 남아있습니다.
49
49
50
-
Let's compare it with `||`:
50
+
이제 `??`와 `||`을 비교해봅시다.
51
51
52
52
```js run
53
53
let height =0;
@@ -56,62 +56,62 @@ alert(height || 100); // 100
56
56
alert(height ??100); // 0
57
57
```
58
58
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`을 할당하는 것과 유사한 유스케이스에선 이처럼 `||`는 부정확한 결과를 일으킬 수 있습니다.
60
60
61
-
The`height ??100` returns `100` only if `height` is exactly `null` or `undefined`.
61
+
대신`height ??100`은 `height`가 정확히 `null`이나 `undefined`일때만 `height`에 `100`을 할당합니다.
62
62
63
-
## Precedence
63
+
## 연산자 우선순위
64
64
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`로 꽤 낮습니다.
66
66
67
-
That's lower than most operators and a bit higher than `=` and `?`.
67
+
대부분의 연산자보다 우선순위가 낮고, `=`와 `?` 보다는 조금 높죠.
68
68
69
-
So if we need to use `??` in a complex expression, then consider adding parentheses:
69
+
따라서 복잡한 표현식 안에서 `??`를 사용할 땐 괄호를 추가해주는 게 좋습니다.
70
70
71
71
```js run
72
72
let height =null;
73
73
let width =null;
74
74
75
-
//important: use parentheses
75
+
//괄호를 추가!
76
76
let area = (height ??100) * (width ??50);
77
77
78
78
alert(area); // 5000
79
79
```
80
80
81
-
Otherwise, if we omit parentheses, then `*` has the higher precedence and would run first. That would be the same as:
81
+
`*`가 `??`보다 우선순위가 높기 때문에 괄호를 생략하게 되면 `*`가 먼저 실행되어 아래 코드처럼 동작할 겁니다.
82
82
83
83
```js
84
-
//not correct
84
+
//원치 않는 결과
85
85
let area = height ?? (100* width) ??50;
86
86
```
87
87
88
-
There's also a related language-level limitation. Due to safety reasons, it's forbidden to use `??` together with `&&` and `||` operators.
88
+
`??`엔 자바스크립트 언어에서 규정한 또 다른 제약사항이 있습니다. 안정성 관련 이슈 때문에 `??`는 `&&`나 `||`와 함께 사용하는 것이 금지되어 있습니다.
89
89
90
-
The code below triggers a syntax error:
90
+
아래 예시를 실행해봅시다. 문법 에러가 발생합니다.
91
91
92
92
```js run
93
-
let x =1&&2??3; //Syntax error
93
+
let x =1&&2??3; //SyntaxError: Unexpected token '??'
94
94
```
95
95
96
-
The limitation is surely debatable, but for some reason it was added to the language specification.
96
+
이 제약에 대해선 아직 논쟁이 많긴 하지만 어쨌든 이슈가 있어서 명세서에 제약이 추가된 상황입니다.
97
97
98
-
Use explicit parentheses to fix it:
98
+
에러를 피하려면 괄호를 사용해주세요.
99
99
100
100
```js run
101
-
let x = (1&&2) ??3; //Works
101
+
let x = (1&&2) ??3; //제대로 동작합니다.
102
102
alert(x); // 2
103
103
```
104
104
105
-
## Summary
105
+
## 요약
106
106
107
-
- The nullish coalescing operator `??` provides a short way to choose a "defined" value from the list.
107
+
- null 병합 연산자 `??`를 사용하면 피연산자 중 '값이 할당된' 변수를 빠르게 찾을 수 있습니다.
108
108
109
-
It's used to assign default values to variables:
109
+
`??`는 변수에 기본값을 할당하는 용도로 사용할 수 있습니다.
110
110
111
111
```js
112
-
//set height=100, if height is null or undefined
112
+
//height가 null이나 undefined인 경우, 100을 할당
113
113
height = height ??100;
114
114
```
115
115
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
+
- `??`의 연산자 우선순위는 대다수의 연산자보다 낮고 `?`와 `=` 보다는 높습니다.
0 commit comments