5 ms·
Your rephrasing is better. For example, there's no reason to do: typeof someVar === 'number' ...because `typeof` always returns a string, by spec[1] 1. h
by Xophmeister 12y ago
Your rephrasing is better. For example, there's no reason to do:
typeof someVar === 'number'
...because `typeof` always returns a string, by spec[1]
1. http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3 http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3
- Drakim 12y agoMight the === comparison be faster, since the underlying JavaScript engine doesn't need to determine how the two types should be cast back and forward to be compared?
- Xophmeister 12y agoYep, === will be quicker if the types are the same.
- mattdesl 12y agoWell, there is a reason: consistency. ;) I hate seeing "==" in code because it's rarely clear whether the programmer intends it to coerce or just didn't bother with the third keystroke. So "===" is just enforced everywhere through jshint.
- nawitus 12y agoIt's probably easier to set a linter to disallow "==" equality comparison globally instead of only when "===" is strictly necessary.
- SamReidHughes 12y agoYes there is a reason, because somebody reading the code is going to have to wonder why the heck you used a double-equals, and the code could be edited later in such a way that the triple-equals becomes relevant again.
- hajile 12y agoRemoving a single character is hardly worth the issues that may arise from not using it. The only quasi-acceptable use of '==' IMHO is: myVar == null as it is a little easier than: myVar === undefined || myVar === null That said, I use only '===' as I don't want to ever worry about my code (plus, the use of a maybe monad makes this and other problems go away).
- paracaidista 12y ago== vs. === aside, it's a bit hypocritical to use === because it's safer when you're using undefined as a keyword. You can't tell just from looking at that line whether the variable undefined is actually undefined or not; it could, in fact, be defined (below the global scope, that is -- unless you're in an older browser, I suppose). Granted, defining a variable called undefined would be absurd, and I realize I'm nitpicking here, but if we're trying to make a point about good practice, typeof would be the way to go. (Of course, more often you only really care if something == null or not, so you'd just do a simple == null check, like you said.)