> For the complete documentation index, see [llms.txt](https://radlohead.gitbook.io/typescript-deep-dive/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://radlohead.gitbook.io/typescript-deep-dive/recap/equality.md).

# 비교 연산자

## 원시데이터에서 동등연산자와 일치연산자 차이

자바스크립트에서 조심해야 할 차이가 있습니다. `==` 와 `===`. 자바스크립트가 동등연산자를 사용할 때 탄력적으로 시도하기 때문에 `==` 아래의 동등연산자와 일치연산자에서 동등연산자는 탄력적으로 `string`을 `number`로 변환합니다.

```javascript
console.log(5 == '5') // true   , TS Error
console.log(5 === '5') // false , TS Error
```

자바스크립트에서 동등연산자와 일치연산자중에서 선택하는 것은 항상 이상적인 것은 아닙니다. 왜냐하면 아래 예제에서 두 명령문은 모두 false에 해당합니다. `""` 와 `"0"` 둘 다 string이지만 같지 않습니다. 두번째는 0을 포함하고 있지만 공백으로 인식하기 때문에 (`""`) 힝상 falsy를 반환합니다. 그러므로 두 문장은 `==`를 사용할때는 동등하고 `===`를 사용할때는 항상 false가 됩니다.

```javascript
console.log('' == '0') // false
console.log(0 == '') // true

console.log('' === '0') // false
console.log(0 === '') // false
```

> Note `string == number` 와 `string === number` 타입스크립트에서 동등연산자와 일치연산자는 컴파일 단계에서 에러를 출력하기 때문에 이런 부분에 대해서 걱정할 필요가 없습니다.

비슷한걸로는 `==` vs. `===`, 그리고 `!=` vs. `!==`가 있습니다.

꿀팁: 항상 `===` 와 `!==` 이것을 사용하고 null체크는 동등연산자를 사용해도 됩니다.

## 참조데이터에서 동등연산자와 일치연산자 차이

만약 당신이 두개의 객체를 비교하려고 한다면 `==`/`===`로는 충분히 비교하기 어렵습니다.

```javascript
console.log({ a: 123 } == { a: 123 }) // False
console.log({ a: 123 } === { a: 123 }) // False
```

깊은 비교를 사용하려면 [깊은 비교](https://www.npmjs.com/package/deep-equal) 이 패키지를 참고하십시요

```javascript
import * as deepEqual from 'deep-equal'

console.log(deepEqual({ a: 123 }, { a: 123 })) // True
```

당신은 항상 깊은 비교를 할 필요가 없습니다. 그럴때는 `id`값을 체크하면 됩니다.

```typescript
type IdDisplay = {
    id: string
    display: string
}
const list: IdDisplay[] = [
    {
        id: 'foo',
        display: 'Foo Select'
    },
    {
        id: 'bar',
        display: 'Bar Select'
    }
]

const fooIndex = list.map(i => i.id).indexOf('foo')
console.log(fooIndex) // 0
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://radlohead.gitbook.io/typescript-deep-dive/recap/equality.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
