-8

I have seen many uses of "===" in conditional statements.

Can anyone tell me what does it mean?

something like ternary operator?

if(typeof(x) == "string")
{
    x= (x=== "true");
}
The Marlboro Man
  • 951
  • 7
  • 21
Sankarganesh Eswaran
  • 9,425
  • 3
  • 20
  • 23

5 Answers5

3

The === operator checks for equality which means that the type and value are the same. The == operator checks for equivalence which means that the value is the same and it disregards the type.

Example

alert("1" == 1); //alerts true
alert("1" === 1); //alerts false, types are different.
alert(1 === 1); //alerts true

This can be useful in Javascript due to the loosely typed nature of the language and the truthy/falsey nature of variables.

For example, an empty String is == false

("") ? alert(true): alert(false); //alerts false

You will also find that 0 is == false

(0) ? alert(true): alert(false); //alerts false

As well as an empty property on an object:

({}.prop) ? alert(true): alert(false); //alerts false

In these situations it may be necessary to use the === operator when type is important.

Kevin Bowersox
  • 90,944
  • 18
  • 150
  • 184
2

It's strict equality comparison. It means that not just the value is evaluated but also the type of the objects. More info is available in the ECMAScript-specification.

Karl-Johan Sjögren
  • 15,390
  • 7
  • 60
  • 66
0

The === mean "equality without type coercion". Using the triple equals both the values and their types must be equal.

federico-t
  • 11,595
  • 17
  • 64
  • 109
0

"===" does not perform type conversion so it could have a different result than "==".

waterplea
  • 3,162
  • 5
  • 26
  • 45
  • You mean type checking, instead of type conversion ? – Aazim Nov 06 '19 at 19:21
  • No, I mean type conversion. `==` converts types so it can compare them, `===` does not do that and compare as is. Therefore `'' == false` is `true` whereas `'' === false` is `false` – waterplea Nov 11 '19 at 16:30
0

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

Arpan Buch
  • 1,290
  • 4
  • 19
  • 41