Whats the different between two (`==`) and three (`===`) equal signs?

Why are there three === instead of two?

3 equals signs is known as Identity / strict equality, meaning that the data types also must be equal:

console.log(1 == true) // output: true
console.log(1 === true) // output: false
68 Likes

For anyone still interested - there is a great explanation with examples here

58 Likes

what about a single (=) equals? In this module 7 (number) does seem to = 7 (number) and also “7” (string). I understand why you would use triple equals, but why NOT use single as opposed to double?

4 Likes

= is assignment.
== check if values are equal:

3 == "3" // true

=== also check if the data type matches:

3 === "3" // false
68 Likes

Okay, I see. I wasn’t actually comparing 7 (number) to 7(number) it was redefining the variable, and in this case they did just so happen to actually be equal. When I reran the premise just now I found that the following IF evaluated as true (or perhaps more technically, the variable sven had a truthy value?).

let sven = 7;
if (sven='taco') {
  console.log(sven)
}

The output to the console was ‘taco’.

So now this is clear to me. Thanks for the feedback!

2 Likes

Thanks for sharing this article!
:ok_hand:

3 Likes

The “===” stands for “equal to” whiles the “==” stands for equivalence.

2 Likes

No caso desse exemplo => ‘apples’ === ‘oranges’ // false qual foi o criterio para resultado ser falso?como se chegou a esse resultado ok ambos sao strings e ‘apples’ != ‘orange’

1 Like

thank you!!! <3 <3 <3

1 Like

thank you so much!!!

1 Like

Thank you! This answer is very clear!

1 Like

== is an Equality operator and performs abstract comparison by doing type coercion before comparing i.e Converts the variable to same datatype then compares
=== is an Identity operator and performs strict comparison i.e Does not convert to same data type.

/*
This code compares two values with different datatypes i.e string and number
Expect true using == and false ===
*/

let a = 2020
let b = "2020"
//This result wont show up because the values are not of same type
if (a === b) {
    console.log(`strict a: ${a}`)
    console.log(`strict b: ${b}`)
} 
//This result will show up because b will be converted to number before comparison. 
if (a == b) {
    console.log(`abstract a: ${a}`)
    console.log(`abstract b: ${b}`)
}
6 Likes

Thank you, this is a great clear comparison of the three variations of the equals

1 Like

console.log(3==“3”) // compares the data between the (), which is 3.

console.log(3===“3”) // compares the data type, number vs string.

So it seems to me that ‘===’ is the correct operator to use in all cases, because I don’t see a scenario where I want to compare different data types (in human language - totally different things, like tables and chairs).
If the types are different, then there is a bug somewhere. At the worst case if I have different data types intentionally, I should convert them to the same type in order to show that it is intentional and not a bug.
Ooooor? am I mistaken and there are examples when I need to use ‘==’?

=== is absolute preferred.

no. JavaScript interacts with webpages, retrieving values from a webpage will give a string, even when the value is a number. But then conversion is preferable I think

some developers are lazy and use ==.

Oi, não sei se eu entendi a pergunta. Mas o que entendi das outras respostas é que como são 3 sinais de igual, é o “igualdade estrita” (strict equality). Ou seja, tem que ser o mesmo tipo de dado (data type) e também o mesmo valor.
O tipo de dado no exemplo é texto (string). Tanto o texto ‘apple’ (maçã) quanto o texto ‘orange’ (laranja) são textos. Então nesse ponto, são iguais. MAS maçãs são diferente de laranjas. Então não existe uma igualdade, entende?
Pelo que entendi, a comparação de igualdade é bem literal. Então se um texto fosse “maçãs” e o outro fosse “maçãs.” << esse ponto ali dentro das aspas já ia ser considerado um texto diferente.

Aí no caso da “identidade” (identity), ele só compara se o valor é igual ou não, independente do tipo de dado. Quando a gente escreve entre aspas, o tipo de dado é interpretado como string (texto). Então se eu declarar uma variável como:

let tipoString = “7”;

ele vai interpretar esse sete como um texto. agora se eu declarar a variável como:

let tipoNumero = 7;

ele vai interpretar esse 7 como um número. São dois tipos de dados diferentes, números e textos. Se eu usar a comparação de identidade == ele vai dizer: são iguais. Mas se eu usar a comparação de “igualdade estrita” === ele vai dizer que são diferentes, pq os valores batem, mas os data types não batem.

Tentei ser bem detalhada, espero que ajude :slight_smile:

Nice and simple explanation. Thanks

Would variable b get converted to a number, or would variable a be converted to a string?