What is the difference between operators '==' and '==' in Javascript?

Asked

Viewed 13,140 times

8

I would like to know the difference between the two operators who would be:

(1): ==

(2): ===

If possible, I would also like to understand in this case the use of the opposite operators of them:

(3): !=

(4): !==

What would be the difference between operators:

1:

if (foo == "foo")

2:

if (foo === "foo")

And what would be the difference between the opposite operators:

3:

if (foo !== "foo")

4:

if (foo != "foo")

Could someone please inform me?

  • The question is different, because the OP is asking because in one case returns false and other true, but not focused on the real meaning of operators, which is what I wish to know.

  • 3

    @Pauloroberto The answer is the same, I consider yes duplicate.

  • Okay, you can consider a duplicate, but the question is different, and the answer I need is not found in the answers in your "duplicate"

  • http://meta.pt.stackoverflow.com/questions/607/lidando-com-perguntas-duplicadas

  • @Bigown ok, so this is a similar duplicate, different from the exact one, so the question should remain open.

  • It seems that most disagree. No one is even considering reopening.

  • 2

    I edited the other question to get a little more generic. Why do you think the answers there do not meet you?

Show 2 more comments

4 answers

16


The difference is that if used == there will be a coercion of the value for both sides of the expression to have the same type.

In the case of === there will be no coercion and so the code below will be false:

if(1 === '1')
    console.log('igual');
else
   console.log('diferente');//Esta será a resposta

Already if we use only == there will be a coercion for both to be the same type and will give equal.

if(1 == '1')
    console.log('igual');//Esta será a resposta
else
   console.log('diferente');

Another example:

if(0 == '')
    console.log('igual');//Esta será a resposta
else
   console.log('diferente');

In the case '' is considered a value falsey, which can be considered false even if it does not have the value false.

In the case below is given the correct message, which are different:

if(0 === '')
    console.log('igual');
else
   console.log('diferente');//Esta será a resposta

Edited

Here are some other values that may result in strange cases in the logical comparison in javascript:

False report:

0
''
' '
null
undefined
NaN
  • Would you like a more specific explanation, do you have this knowledge? I would be happy to mark the question as correct if you specify.

  • Thank you, I would also like to know one thing if possible: when using === it compares the two things at the same time ? Type and Value? in case of 0 === '' javascript understands '' as false and 0 as false. If I used == he would also understand in the same way?

  • 1

    Actually if you use the === he will do what would be most obvious to my mother :) . Say they are different. Imagine you testing if a variable is false. If you use === no problem but '' == false will say it is true. There are several values in javascript that cause this confusion.

  • Great explanation this "obvious to my mother" means the pure understanding that is different, already the === provides for a more internal checking, Typing, perfect.

  • Damn! The one from ' ' caught me by surprise: if ( ' ' ) consider ' ' as true, but if ( ' ' == false ) consider ' ' as false!

6

== makes value-only comparison

=== makes value and type comparison

if (1 == "1") // retorna true
if (1 === "1") // retorna false

Same thing with =! and ==! only the command is negation, returns true if the value is different and/or the type is also different.

if (1 != "1") // retorna false
if (1 !== "1") // retorna true

2

The difference is that the type of the value is checked.

Ex:

var foo = "10";

console.log(foo == "10"); //true
console.log(foo === 10); //false

1

== test if it is equal
=== test if it is the same and of the same type

  • The test 0 == '' will give true. Different types and different values.

  • 1

    JS considers 0 = '. testing 1 == '1' returns true. testing 1 === '1' returns false. the first tests value, the second tests value and type.

  • In my previous comment gave true because the value is equal or because the type is equal? Neither of the two, even so gave true.

  • @Vitorcanova JS considers 0 and '' as "falsy". if (0), if (''), if (undefined), if (false) give them all the same. A lot of people don’t like them, but yes, they’re the same (for JS using coercion)

  • @Juanmendes Yes, I will add in my answer the falsey and truthy that I know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.