!! 1 ==“1”이 true이고 !! 2 ==“2”가 false 인 이유는 무엇입니까?
제목에서 알 수 있듯이 이유는 다음과 같습니다.
> !!1=="1"
같은
True
과
> !!2=="2"
같은:
False
마찬가지로, 이유는 무엇입니까 > "1"==true
동일 true
과 > "2"==true
동일false
나는 당황 스럽다. 이것들은 JS의 버그입니까 아니면 여기서 무슨 일이 일어나고 있습니까?
당으로 연산자 우선 순위의 규칙, 논리는 !
이상 더 높은 우선 순위를가집니다 ==
. 따라서 두 경우 모두 !!
먼저 평가됩니다.
참고 : 다양한 물체의 진실성은 이 대답 에서 설명되었습니다 .
첫 번째 사례
!!1 == "1"
!1
에 평가 될 것입니다 false
때문에, 1
Truthy 간주됩니다. 다시 부정하면 true
. 그래서 표현은
true == "1"
이제 ECMAScript 5.1 사양에 정의 된 The Abstract Equality Comparison Algorithm에==
따라 평가 되는 연산자를 사용한대로 강제 규칙이 시작됩니다 .
만약 6.
Type(x)
되고Boolean
, 비교의 결과를 반환ToNumber(x) == y
.
따라서 부울 값에 대한 알고리즘에true
따라 1 인 숫자로 변환 ToNumber
됩니다 . 이제 표현은
1 == "1"
지금,
4. 만약이
Type(x)
있다Number
하고Type(y)
있다가String
, 비교의 결과를 반환x == ToNumber(y)
.
따라서 "1"
는 숫자로 변환되고 ToNumber
알고리즘에 따라 1이 됩니다 . 이것이 true
첫 번째 경우에 나타나는 이유 입니다.
두 번째 사례
여기에도 동일한 규칙이 적용됩니다.
!!2 == "2"
된다
true == "2"
그때
1 == "2"
이것은
1 == 2
이것이 true
두 번째 사례가 인쇄되는 이유 false
입니다.
tldr; 이는 연산자 알고리즘 의 [ToNumber] 변환 때문입니다 ==
.
첫 번째 단계는 표현을 단순화하는 것입니다. !!x=="x"
와 같이 구문 분석 되기 때문에 평등에 대한 실제 관련 표현식은 다음 (!!x)=="x"
과 같습니다.!!a_truthy_expression -> true
!!1=="2" -> true=="1" -> Boolean==String
!!2=="2" -> true=="2" -> Boolean==String
따라서 11.9.3 The Abstract Equality Comparison Algorithm 의 규칙을 살펴보고 응용 프로그램과 함께 다음과 같은 결과 를 얻습니다.
규칙 6-Type (x)가 Boolean이면 비교 결과를 반환합니다. ToNumber (x) == y.
한 결과 Number==String
, 각각, 1 == "1", 1 == "2" 1 . 그런 다음 규칙
규칙 7-Type (x)가 Number이고 Type (y)가 String이면 비교 결과를 반환합니다. x == ToNumber (y).
is applied which results in Number==Number
or 1==1 and 1==2, respectively1; the latter is clearly false.
Rule 1 - If Type(x) is the same as Type(y), then [by c.iii.] If x is the same Number value as y, return true [else return false].
(The same algorithm explains the String==Boolean
case when the complementing rules are applied.)
1To see the [ToNumber] rule applied, consider:
+false -> 0
+true -> 1
+"1" -> 1
+"2" -> 2
Its a precedence operator problem.
The !
operator is an unary operator. That means the left side must be an expression or a boolean evaluable section. See Javascript MDN.
!!1==1 is not necessary !!(1==1)
!!2==2 is not necessary !!(2==2)
I think that these expressions should be consistent if the equal operator has more precedence than ! operator. But if we consider the opposite, evaluating first negations we have:
!!1 == 1
!1 -> false
!!1 -> true
!!1 == 1
And with the two
!!2==2
!2 -> false
!!2 -> true
(!!2) == 2 -> false
That is because the ! operator has precedence over == operator
See Mozilla Operator Preference
!!1
is equal to true, and "1" is equal to true ("0" is false, so is every other string). So !!1 == "1"
evaluates to true == true
, which of course returns true.
!!2
is also equal to true. As I mentioned earlier, "2" is not "1", so it's false. Therefore, we have true == false
, which of course returns false.
If you want to see if 2 (a number) is equal to "2" (a string representation of a number), then all you have to do is 2 == "2"
, which evaluates to 2 == 2
, which is true. The difference is that we're not comparing a boolean against a boolean. We're comparing a number against a number.
Basically, putting !!
in front of a number converts to a boolean, which forces JavaScript to cast your string to a boolean instead of a number.
Because "1" may be considered as "true" when you do equality check, not identity, but "2" - can't.
참고URL : https://stackoverflow.com/questions/23693089/why-does-1-1-equal-true-and-2-2-equal-false
'Development Tip' 카테고리의 다른 글
이름 "XYZ"가 네임 스페이스 "clr-namespace : ABC"에 없습니다. (0) | 2020.11.12 |
---|---|
녹색에서 빨간색으로 백분율에 따라 다름 (0) | 2020.11.12 |
버튼으로 div 표시 / 숨기기 전환? (0) | 2020.11.12 |
C ++ 11의 원시 문자열 리터럴 R“(…)”에서 괄호를 사용하는 이유는 무엇입니까? (0) | 2020.11.11 |
테이블의 모든 행을 어떻게 반복 할 수 있습니까? (0) | 2020.11.11 |