Search code examples
javascripttypeof

Why === instead of == for undefined


Several sources suggest, that there are two canonical ways to check whether variable is undefined:

foo === undefined
typeof foo === 'undefined'

But can anyone explain, why would one use === instead of == ?

EDIT: the question is not about === vs ==. It is about using correct operator with the 'undefined'. The difference between === and == is obvious. But the question is which operator would be more correct when checking if value is undefined or not.


Solution

  • Sure simple. You base it off which behaviour you want (below)

    null == undefined // true
    undefined === null // false
    typeof undefined // 'undefined'
    typeof null // 'object'