开发者

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

开发者 https://www.devze.com 2023-01-17 23:16 出处:网络
Throughout many third-party libraries and best practices blogs/recommendations, etc... it is common to see syntax like this:

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 == sh开发者_Go百科ould 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...


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


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.


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).


Because === is quicker than ==, due to omitting type coercion. Sure it is probably a negligible difference but it is still there.


Triple equal operators are mostly used for variable type and value checking (all in 1 expression), also known as equality without type coercion.

Example:

var a = 1;
var b = 1;
var c = "1";
var d = "1";

alert (a === b); //True, same value and same type (numeric)
alert(c === d); //True, same value and same type (string)
alert(b === c); //False, different type but same value of 1

See Doug Crockford's YUI Theater on type coercion.


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.

The most efficient reason for not using typeof and rather the === operator would be for type coercion (interpretation) between browsers. Some browsers can pass 6=="6" as true and some wouldn't (depending on the strictness of the JS interpreter) so by introducing type coercion would clarify this. Also, it would bring the "Object-Orientativity" approach to it since JavasScript's variables are not type-based variables (i.e. variable types are not declared on compile time like in Java).

E.g. in Java, this would fail:

if ("6" instanceof Number) { // false

Hope I answered your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消