2.9 Comparison

히진로그·2022년 1월 27일
0

Modern-Javascript

목록 보기
6/14

출처: https://javascript.info/

  • 혼자 읽고 타이핑하며 이해하면서 공부한 흔적 남기기

We know many comparison operators from maths.

In JavaScript they are written like this:

  • Greater/less than: a > b, a < b
  • Greater/less than or equals: a >= ba <= b.
  • Equals: a == b, double equality sign == means the equality test.
  • Not equals: a != b

A comparison result can be assigned to a variable, just like any value:

let result = 5 > 4; 
alert(result); // true

String comparison

To see whether a string is greater than another, JavaScript uses the so-called "dictionary" or "lexicographical" order.

자바스크립트는 스트링의 대소비교를 위해 사전적 또는 사전 편집적 순서를 사용한다.

strings are compared letter-by-letter.

앞 순서일수록 작고 뒷 순서일 수록 크다.

Comparison of different types

When comparing values of different types, JavaScript converts the values to numbers.

다른 타입들의 값을 비교할 때, 자바스크립트는 값을 숫자로 변환한다.

Strict equality

A regular equality check == has a problem. It cannot differentiate 0 from false.

console.log(0 == false); // true
console.log('' == false); // true
console.log('' === false); // false

This happens because operands of different types are converted to numbers by the equality operator == .

A strict equality operator === checks the equality without type conversion.

console.log(0 === false); // false

because the types are different.

There is also a 'strict non-equality' operator !== analogous to !=.

Comparison with null and undefined

There's a non-intuitive behaviour when null or undefined are compared to other values.

null과 undefined를 다른 값과 비교할 때 직관적이지 않은 동작이 있다.

For a non-strict check ==

console.log(null == undefined); // true

null/undefined are converted to numbers: null becomes 0, while undefined becomes NaN.

굉장히 헷갈림😵‍💫

Strange result: null vs 0

console.log(null > 0); // false (1)
console.log(null == 0); // false (2)
console.log(null >= 0); // true (3)

수학적으로보면 이상하다. (3)에서 null이 0보다 '크거나 같다'라는 뜻은 그 위 조건(1, 2)에서 적어도 하나는 true여야 하는데, 두 조건 모두 false이다.

The reason is that an equality check == and comparison > < >= <= work differently.

같음을 체크하는 것과 값을 비교하는 기호는 다르게 동작하기 때문이다.

비교 기호는 null을 숫자로 변환하며 0처럼 취급한다. 그래서 마지막 조건에서 true 가 나왔고 첫 번째 조건에서는 false가 나온 것이다.

그에 반해, undefined와 null에 대한 equality check == 은 변환없이, 그들 서로는 같고(they equal each other) 다른 것과는 같지 않다. (2) 조건이 false인 이유이다.

An incomparable undefined

console.log(undefined > 0); // flase (1)
console.log(undefined == 0); // flase (2)
console.log(undefined >= 0); // flase (3)

We get these results because:

  • Comparisons (1) and (2) return false because undefined gets converted to NaN and NaN is a special numeric value which returns false for all comparisons.

undefined는 NaN으로 변환되는데, 이는 모든 비교에서 false값을 리턴하는 스페셜한 numeric value이다.

  • The equality check (3) returns false because undefined only equals null, undefined, and no other value.

(3)조건이 false를 리턴하는 이유는 undefined는 오직 null과 자기자신(undefined)과의 비교에서만 같음을 리턴하기 때문이다.

⇒ The values null and undefined equal == each other and not equal any other value.
즉, null과 undefined는 서로에게만 같고 다른 값과는 같지 않다.

0개의 댓글