자바스크립트에서 =, ==, ===의 차이점

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

반응형