Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

typeof x === 'object' (instead of typeof x == 'object') typeof y === 'string' (instead of typeof x == 'string') typeof z === 'function' (instead of typeof x == 'function') 

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.

Under what circumstances will typeof not return a string literal? And even if there's some fringe case why is the additional type check being used for object, string, function, etc...

like image 812
Eric P Avatar asked Sep 27 '10 13:09

Eric P


People also ask

Why you should use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

What is the value of typeof undefined === typeof null?

Referencing undeclared variables usually results in a ReferenceError, except when using the typeof keyword. The typeof undefined is the string "undefined" — and undefined is a falsy value that is loosely equal to null but not to other falsy values.

What does typeof do in JavaScript?

typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well.

Which of the following is true about typeof in JavaScript?

Explanation. Both of the above options are correct. Q 9 - Which of the following is true about typeof operator in JavaScript? A - The typeof is a unary operator that is placed before its single operand, which can be of any type.


2 Answers

To answer the main question - there is no danger in using typeof with ==. Below is the reason why you may want to use === anyway.


The recommendation from Crockford is that it's safer to use === in many circumstances, and that if you're going to use it in some circumstances it's better to be consistent and use it for everything.

The thinking is that you can either think about whether to use == or === every time you check for equality, or you can just get into the habit of always writing ===.

There's hardly ever a reason for using == over === - if you're comparing to true or false and you want coercion (for example you want 0 or '' to evaluate to false) then just use if(! empty_str) rather than if(empty_str == false).


To those who don't understand the problems of == outside of the context of typeof, see this, from The Good Parts:

'' == '0'          // false 0 == ''            // true 0 == '0'           // true  false == 'false'   // false false == '0'       // true  false == undefined // false false == null      // false null == undefined  // true  ' \t\r\n ' == 0    // true 
like image 184
Skilldrick Avatar answered Sep 23 '22 17:09

Skilldrick


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.

like image 23
Matthew Flaschen Avatar answered Sep 24 '22 17:09

Matthew Flaschen