Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need this special === operator?

I've used C++ and Java before and they don't have this === operator.

How come they manage without it but in languages like PHP its key.

like image 484
user448363 Avatar asked Sep 20 '10 07:09

user448363


People also ask

What is the use of === operator?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

Why do we use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

What does === mean?

The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.

What is the === operator and explain with an example?

=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value.


2 Answers

Actually equals in Java and == in C# act like === does in php. I.e. "24".equals(24) will return false.

What java and C# don't have an equivalent of is PHP's == (i.e. an operator/method such that "24".fuzzyEquals( 24 ) would return true). And that's because C# and Java are strongly typed and such an operator would be against their philosophy.

like image 91
sepp2k Avatar answered Sep 22 '22 16:09

sepp2k


Because PHP is not type safe. == compares 2 values, but === compares the values AND checks if their class types are the same.

I believe "2" == 2 returns true, while "2" === 2 returns false.

like image 29
Joachim VR Avatar answered Sep 26 '22 16:09

Joachim VR