본문 바로가기

Programming/JavaScript

Difference Between == and === in JavaScript

        var myNum = 5;
        var otherNum = 6;
        var notNum = "5";

        if(myNum == notNum){
            console.log("true");
        }
        else {
            console.log("false");
        }

myNum은 int이고

notNum은 string이지만

타입 변환(Type coercion)이 돼서 결국 myNum과 notNum이 같은 것으로 여겨짐

-> true값이 나옴

 

Type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers).

https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion

 

Type coercion - MDN Web Docs Glossary: Definitions of Web-related terms | MDN

Type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers). Type conversion is similar to type coercion because they both convert values from one data type to another with one key difference

developer.mozilla.org

 

        if(myNum === notNum){
            console.log("true");
        }
        else {
            console.log("false");
        }

하지만 myNum과 notNum이 strictly equal은 아니기 때문에

=== Triple equals로 하면 false값이 나옴

Strict equality (===)

'Programming > JavaScript' 카테고리의 다른 글

Sequence selection loop  (0) 2022.03.09
While Loops  (0) 2022.03.09
JavaScript treats variables that are undefined as false.  (0) 2022.03.08
Arrays  (0) 2022.03.08
typeof  (0) 2022.03.08