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

后端 未结 5 1891
执笔经年
执笔经年 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条回答
  •  旧时难觅i
    2020-11-29 09:46

    There's no reason at all to favour === over == in this case, since both operands are guaranteed to be strings and both operators will therefore give the same result. Since == is one character fewer I would favour that.

    Crockford's advice on this is to use === all the time, which is reasonable advice for a beginner but pointlessly paranoid if you know the issues (covered in other answers).

提交回复
热议问题