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


Many third-party libraries and best practices during blogs / recommendations etc. ... This is normal to see syntax like this. Type x === type 'object' (instead of typeof x == 'object') y === 'string' (instead of typeof x == 'string') typeof z === 'function' (typef x == 'instead of' function ')

If the type of operator already gives the string, then what is the need to check the return value? If typeof (x) is always string , even if it is actually x, then == should be sufficient and === is unnecessary.

Under what circumstances will no return a string? And even if there is some fridge case, why the extra type of investigation is being used for objects, strings, functions etc. ...

To answer the main question - there is no risk of using type = with typeof . Below is also one of the reasons that you want to use === .


Crockford recommends that it is safe to use === in many situations, and if you are going to use it in certain circumstances, So it is better to be compatible and use it for everything.

The thought is that you can think of using either == or === every time you check the equality , Or you can always come in the habit of writing === . There is never a single reason to use

== more === - if you type true or Comparing with false and you want to forcibly (for example, if you have 0 or ' to incorrect ), Then simply .


does not understand the problems, == outside the context of type, see:

  '' = = '0' // wrong 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == Faucet / False null == Undefined // true '\ t \ r \ n' == 0 // true  

Comments