변수로 선언된 10 "10" 이 true로 나오는 이유가 무엇인가요?
조회수 568회
이제 막 js를 입문하였습니다
그 중에 너무 궁금해서 여쭤봅니다.
var c = 10;
var d = "10";
document.getElementById("dd").innerHTML = Boolean(c == d);
==
이 같은 값으로 인식해서 true
가 되는걸로 알고 있습니다.
(===
이건 타입까지 구분하니까 이해가 되는데..)
정확히 10(Number)
과 "10"(String)
이 어떻게 값을 정확히 인식하는거져?
내부에 들어가 있는 숫자 10을 인지해서 그런가요?
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
2 답변
-
The standard equality operators (
==
and!=
) use the Abstract Equality Comparison Algorithm to compare two operands. If the operands are of different types, it will attempt to convert them to the same type before making the comparison, e.g., in the expression5 == '5'
, the string on the right is converted to Number before the comparison is made.번역해 보면: (용어가 정확하지 않을 수 있으니 주의)
표준 등가 연산자(
==
,!=
)는 추상 등가 비교 알고리즘을 이용해서 양변을 비교한다. 양변의 타입이 다르다면, 비교를 하기 전에 같은 타입으로의 변환을 시도한다. 예를 들어,5 == '5'
라는 표현식은 우변의 스트링이 먼저 숫자로 형변환된 이후에 비교가 이루어진다. -
댓글 입력