2

Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?

In Douglas Crockford's book Javascript: The Good Parts, it is recommended to not use == at all, due to hard to memorized rules. Do advanced or seasoned Javascript programmers really not use == or !=?

If so, then I guess we will be using === and !==, but then how do we use them effectively? Is the most common case comparing a string with number, so we can always do

if (Number(s) == 3) { ... }       // s is a string

Can Number(s) work in most browsers? And what are the other common cases to use with ===?

Community
  • 1
  • 1
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    Well... I use `==` when I'm sure it doesn't matter, but always using `===` won't hurt. – Jared Farrish Oct 02 '11 at 16:11
  • I mostly use == although if boolean expressions are involved, i use ===. I read somewhere that == is also less system resourceful than === – Christian Oct 02 '11 at 16:11
  • `Number(s)` is defined in [ECMA-262](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf) (section 15.7, page 154). So yeah, it should work in all modern browsers. – NullUserException Oct 02 '11 at 16:12

4 Answers4

5

The problem with == is that it uses type-coercion which can have unexpected results.

You nearly always want === or !==. You can explicitly change the types as appropriate. In your example it would be easier to write "3" as a string instead of converting the string to a number.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • nearly... can you give an example of the exception? And how about, how to use `===` effectively? – nonopolarity Oct 02 '11 at 16:13
  • @動靜能量: To use `===` effectively in your example you can change the number to a string instead of changing the string to a number. Parsing a string is harder (e.g. what if the client provided a string that does't represent a number?) than converting a number to a string. – Mark Byers Oct 02 '11 at 16:18
1

Never say never, but indeed, in most cases it is best to use the more strict === and !== operators, because they compare the value as well as the type.

Comparing '68' to 68 is not a problem, if it matches, it is probably what you meant. The big risk in not doing so lies especially in 'empty values'. Empty strings may be evaluated as false, as may 0 and 0.0. To prevent hard to find errors, it is best to do a strict type comparison as well.

If you want something to be true or false, it should be true or false and not any other value. Even in cases where these other types would be allowed, it may be better to explicitly convert it to the type you're comparing with, just for the sake of readability, maintanability and clarity. You are in that case making clear that you know the value can be of another type and that it is allowed so. With just using the less strict operator, no one can tell if you just forgot or made a deliberate choice.

So yes, I'd say it's a best practise to always use the strict operators, although there will always be exceptions.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

=== is 'exactly equal to' where == is not exact.

For example,

'' == false // true
0 == false // true
false == false // true

'' === false // false
0 === false // false
false === false // true

== will return true for 'falsy' or 'truthy' values. Read this for more information.

A lot of developers will use ===. It does not hurt to use === solely. But in some cases === is not necessary. Crockford suggests a lot of things in his book. Some people follow his word to the T. Others, myself included, take it all with a grain of salt. He has some good points, but a lot of it is preference.

You should make sure you know exactly what == and === do. And with that information you can decide how to use them, and when.

Marshall
  • 4,716
  • 1
  • 19
  • 14
0

== operator compares two operands values and returns a Boolean value.

=== This is the strict equal operator and only returns true if both the operands are equal and of the same type.

Example:

(2 == '2') //return true
(2 === '2') //return false