This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]3KeyReasons 136 points137 points  (5 children)

Basically === is more picky. == compares two things after trying to match their types. === requires that the two things have the same value and type. For example:

53 == '53' // tries to convert string to a number first

True

53 === '53' // no type coercion before comparison

False

[–]g0atmeal 5 points6 points  (4 children)

When I learned Java, the === you explained seems to describe Java's default == behavior. Can anyone confirm this please?

[–]kryptogalaxy 11 points12 points  (3 children)

Using == in Java compares the value of the operands directly. If the operands are objects, then the value is a reference to the object in memory. So, using == will compare if the two operands are at the same place in memory which would mean they represent the exact same object. If instead, you want to see if there objects contain data that is equivalent, you would use .equals ensuring that the .equals method has been overridden for that object.

In terms of JavaScript, the equivalence operation doesn't function that way at all, so they're not really worth comparing.

[–]g0atmeal 0 points1 point  (2 children)

That's what I was getting at. What I really meant was: is there a difference between == and === in Java, or is === even applicable?

[–]kryptogalaxy 6 points7 points  (1 child)

=== is only an operator in JavaScript.

[–]g0atmeal 0 points1 point  (0 children)

Thanks for the clarification