Skip to content

논리 연산자 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 39 additions & 42 deletions 1-js/02-first-steps/11-logical-operators/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ alert( undefined || null || 0 ); // 0 (all falsy, returns the last value)

1. **변수 또는 표현식 목록에서 첫 번째 truthy 값 얻기.**

Imagine we have several variables which can either contain data or be `null/undefined`. How can we find the first one with data?
데이터를 담거나 `null/undefined`일 수 있는 여러 변수가 있다고 상상해봅시다. 데이터를 가진 첫 번째 변수를 어떻게 찾을 수 있을까요?
데이터를 가졌거나 `null/undefined`일 수 있는 여러 변수가 있다고 상상해봅시다. 데이터를 가진 첫 번째 변수를 어떻게 찾을 수 있을까요?

OR `||`을 사용할 수 있습니다:

Expand All @@ -117,15 +116,14 @@ alert( undefined || null || 0 ); // 0 (all falsy, returns the last value)
alert( name ); // selects "John" – the first truthy value
```

`currentUser`와 `defaultUser` 둘 다 거짓(falsy)이면, `"unnamed"`가 결과가 됩니다.
`currentUser`와 `defaultUser` 둘 다 거짓 같은 값(falsy)이면, `"unnamed"`가 결과가 됩니다.

2. **단락 회로 평가.(Short-circuit evaluation)**

피연산자는 값뿐만 아니라 임의의 표현식이 될 수 있습니다. OR`||`는 왼쪽에서 오른쪽으로 평가 및 테스트합니다. 참인 값에 도달하면 평가가 중지되고 그 값이 리턴됩니다. 이 프로세스는 가능한 한 짧게 왼쪽에서 오른쪽으로 진행되기 때문에 "단락 회로 평가"라고합니다.

"단락 회로 평가"는 두 번째 인수로 주어진 표현식이 변수 할당과 같은 부수적인 효과를 가질 때 분명하게 나타납니다.

In the example below, `x` does not get assigned:
아래 예제에서 `x`는 할당되지 않습니다.:

```js run no-beautify
Expand All @@ -148,20 +146,19 @@ alert( undefined || null || 0 ); // 0 (all falsy, returns the last value)

할당은 간단한 경우입니다. 다른 부수적인 효과도 발생할 수 있습니다.

As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it's false, the second one is evaluated.
우리가 볼 수 있듯이, 그러한 유스 케이스는 "if if를하는 더 짧은 방법"이다. 첫 번째 피연산자는 부울로 변환됩니다. 거짓이면 두 번째 것이 평가됩니다.
보시는 것과 같이, 이러한 사례들은 "`if`를 하는 더 짧은 방법"입니다. 첫 번째 피연산자는 부울로 변환됩니다. 거짓이면 두 번째 것이 평가됩니다.

Most of time, it's better to use a "regular" `if` to keep the code easy to understand, but sometimes this can be handy.
대부분의 경우, 코드를 이해하기 쉽도록 "보통" `if`를 사용하는 것이 더 좋지만, 때때로 편리할 수 ​​있습니다.

## && (AND)

The AND operator is represented with two ampersands `&&`:
AND 연산자는 두 개의 앰퍼샌드`&&`로 표현됩니다.

```js
result = a && b;
```

In classical programming, AND returns `true` if both operands are truthy and `false` otherwise:
클래식 프로그래밍에서 AND는 두 피연산자가 모두 진실이면 `true`를 반환하고, 그렇지 않으면 `false`를 반환합니다.

```js run
alert( true && true ); // true
Expand All @@ -170,7 +167,7 @@ alert( true && false ); // false
alert( false && false ); // false
```

An example with `if`:
`if` 예제 :

```js run
let hour = 12;
Expand All @@ -181,7 +178,7 @@ if (hour == 12 && minute == 30) {
}
```

Just as with OR, any value is allowed as an operand of AND:
OR과 마찬가지로 모든 값이 AND의 피연산자로 할당됩니다.

```js run
if (1 && 0) { // evaluated as true && false
Expand All @@ -190,25 +187,25 @@ if (1 && 0) { // evaluated as true && false
```


## AND finds the first falsy value
## AND는 첫 번째 거짓 같은 값(falsy) 값을 찾습니다.

Given multiple AND'ed values:
AND 연산자의 피연산자가 여러 개있는 경우 :

```js
result = value1 && value2 && value3;
```

The AND `&&` operator does the following:
AND `&&`연산자는 다음을 수행합니다.

- Evaluates operands from left to right.
- For each operand, converts it to a boolean. If the result is `false`, stops and returns the original value of that operand.
- If all operands have been evaluated (i.e. all were truthy), returns the last operand.
- 피연산자를 왼쪽에서 오른쪽으로 평가합니다.
- 각 피연산자를 논리 타입(boolean)으로 변환합니다. 결과가 `false`이면, 평가를 멈추고 그 피연산자의 원래 값을 반환합니다.
- 모든 피연산자가 평가 된 경우 (즉, 모든 값이 참) 마지막 피연산자를 반환합니다.

In other words, AND returns the first falsy value or the last value if none were found.
즉, AND는 첫 번째 거짓 값을 반환하거나 거짓 값을 찾지 못하면 마지막 값을 반환합니다.

The rules above are similar to OR. The difference is that AND returns the first *falsy* value while OR returns the first *truthy* one.
위의 규칙은 OR과 유사합니다. 차이점은 AND가 첫 번째 *거짓* 값을 반환하는 반면 OR은 첫 번째 *참* 값을 반환한다는 것입니다.

Examples:
:

```js run
// if the first operand is truthy,
Expand All @@ -222,37 +219,37 @@ alert( null && 5 ); // null
alert( 0 && "no matter what" ); // 0
```

We can also pass several values in a row. See how the first falsy one is returned:
또한 여러 값을 연속적으로 전달할 수 있습니다. 첫 번째 거짓 값이 어떻게 반환되는지 봅니다.

```js run
alert( 1 && 2 && null && 3 ); // null
```

When all values are truthy, the last value is returned:
모든 값이 참 이면 마지막 값이 반환됩니다.

```js run
alert( 1 && 2 && 3 ); // 3, the last one
```

````smart header="Precedence of AND `&&` is higher than OR `||`"
The precedence of AND `&&` operator is higher than OR `||`.
````smart header="AND `&&`의 우선 순위가 OR `||`보다 높습니다."
AND `&&`연산자의 우선 순위는 OR`||`보다 높습니다.

So the code `a && b || c && d` is essentially the same as if the `&&` expressions were in parentheses: `(a && b) || (c && d)`.
그래서 코드`a && b || c && d`는 본질적으로 `&&`표현식이 괄호 안에있는 것과 같습니다.: `(a && b) || (c && d)`.
````

Just like OR, the AND `&&` operator can sometimes replace `if`.
OR과 마찬가지로 AND `&&` 연산자가 때때로 if를 대체 할 수 있습니다.

For instance:
:

```js run
let x = 1;

(x > 0) && alert( 'Greater than zero!' );
```

The action in the right part of `&&` would execute only if the evaluation reaches it. That is, only if `(x > 0)` is true.
`&&`의 오른쪽 부분에 있는 행위는 평가가 도달 한 경우에만 실행됩니다. 즉, `(x> 0)`이 참인 경우에만.

So we basically have an analogue for:
그래서 기본적으로 다음과 같은 유사점을 가지고 있습니다.

```js run
let x = 1;
Expand All @@ -262,46 +259,46 @@ if (x > 0) {
}
```

The variant with `&&` appears shorter. But `if` is more obvious and tends to be a little bit more readable.
`&&`가 있는 변형된 코드가 더 짧게 나타납니다. 그러나 `if`는 더 명백하고 좀 더 읽기 쉽습니다.

So we recommend using every construct for its purpose: use `if` if we want if and use `&&` if we want AND.
그래서 모든 표현식을 그 것의 목적에 맞게 사용할 것을 권장합니다. if를 원하면`if`를 사용하고 AND를 원하면`&&`를 사용합시다.

## ! (NOT)
## ! (NOT 연산자)

The boolean NOT operator is represented with an exclamation sign `!`.
논리적 NOT 연산자는 느낌표 `!`로 표시됩니다.

The syntax is pretty simple:
구문(syntax)은 매우 간단합니다.

```js
result = !value;
```

The operator accepts a single argument and does the following:
연산자는 단일 인수를 받아들이고 다음을 수행합니다.

1. Converts the operand to boolean type: `true/false`.
2. Returns the inverse value.
1. 피연산자를 논리 타입으로 변환합니다.:`true / false`.
2. 반대의 값을 반환합니다.

For instance:
:

```js run
alert( !true ); // false
alert( !0 ); // true
```

A double NOT `!!` is sometimes used for converting a value to boolean type:
값을 논리 타입으로 변환하는데 double NOT`!!`이 종종 사용됩니다.:

```js run
alert( !!"non-empty string" ); // true
alert( !!null ); // false
```

That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion.
즉, 첫 번째 NOT 연산자는 값을 논리 타입으로 변환하면서 반대되는 값을 반환하고, 두 번째 NOT 연산자는 다시 반대되는 값을 반환합니다. 결국 단순한 논리 타입 변환을 가집니다.

There's a little more verbose way to do the same thing -- a built-in `Boolean` function:
같은 일을 하는 좀 더 장황한 방법, 즉 내장 된 `Boolean` 함수가 있습니다.:

```js run
alert( Boolean("non-empty string") ); // true
alert( Boolean(null) ); // false
```

The precedence of NOT `!` is the highest of all logical operators, so it always executes first, before `&&` or `||`.
`NOT`의 우선 순위는 모든 논리 연산자 중에서 가장 높기 때문에 항상 `&&` 또는 `||` 앞에 먼저 실행됩니다.