The
== operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.
To quote Douglas Crockford's excellent JavaScript: The Good Parts,
Reference:JavaScript has two sets of equality operators:===and!==, and their evil twins==and!=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then===producestrueand!==producesfalse. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:'' == '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 // trueThe lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use===and!==. All of the comparisons just shown producefalsewith the===operator.
http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use
沒有留言:
張貼留言