What's the reason to use === instead of == with typeof in Javascript?

后端 未结 5 1876
执笔经年
执笔经年 2020-11-29 09:23

Throughout many third-party libraries and best practices blogs/recommendations, etc... it is common to see syntax like this:

typeof x === \'object\' (instead         


        
5条回答
  •  时光取名叫无心
    2020-11-29 10:00

    If the typeof operator already returns a string, what's the need to type check the return value as well? If typeof(typeof(x)) is always string, no matter what x actually is, then == should be sufficient and === unnecessary.

    It's subjective. You can just as easily turn this around, and ask, "Why would you use == when you don't expect implicit conversions?" Both work fine here, so use the one you feel expresses your intention better. Try to be consistent within a project.

提交回复
热议问题