자바스크립트에서 =, ==, ===의 차이점
2021. 4. 14. 16:15ㆍ프론트엔드/JavaScript
반응형
정의
= : 대입 연산자
왼쪽의 값을 오른쪽에 대입한다.
== : 비교 연산자
두개의 operand를 같은 타입으로 변환 후 비교한다.
to compare the identity of two operands even though, they are not of a similar type.
=== : 비교 연산자(stricter)
타입(형식)이 다른 경우 false를 반환한다.
ex) 2 === "2" return false
checks that two values are the same or not.
예시
=
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Operators</h2>
<p>a = 2, b = 5, calculate c = a + b, and display c:</p>
<p id="demonstration"></p>
<script>
var a = 2;
var b = 5;
var c= a + b;
document.getElementById("demonstration").innerHTML = c;
</script>
</body>
</html>
Output:
a = 2, b = 5, calculate c = a + b, and display c:
7
==
<!DOCTYPE html>
<html>
<body>
<p id="demonstration"></p>
<script>
var a = 10;
document.getElementById("demonstration").innerHTML = (a == 20);
</script>
</body>
</html>
Output:
false
===
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = 10;
document.getElementById("demo").innerHTML = (x === "10");
</script>
</body>
</html>
Output:
false
* 실무에서는 실수 방지를 위해 대부분 == 보다는 === 사용을 선호
참조 사이트
www.guru99.com/difference-equality-strict-operator-javascript.html
Difference Between =, ==, and === in JavaScript [Examples]
We all use switches regularly in our lives. Yes, I am talking about electrical switches we use for our...
www.guru99.com
반응형
'프론트엔드 > JavaScript' 카테고리의 다른 글
자바스크립트 기본문법 2 (입력과 데이터 가공: 이벤트, 객체) (0) | 2021.04.23 |
---|---|
[Javascript] switch문과 if 문 (0) | 2021.04.16 |
자바스크립트 기본문법 이해하기 1( 객체, 메서드, 매개변수, 프로퍼티, 반환, 변수, 반복, 함수, 배열, while문, for문, for~in문) (0) | 2021.02.15 |
javascript: 정규표현식 (0) | 2021.01.11 |
javascript: event (0) | 2021.01.07 |