Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing, !== versus !=

I know that !== is used to compare variable types too, while != only compares values.

But I see that many people use !== when they compare values, for example:

$stuff = 'foo';
if($stuff !== 'foo') // do...

Is there any reason they do this? Is !== faster than != or what?

like image 660
Alex Avatar asked Jun 15 '11 11:06

Alex


People also ask

What is difference between != and !== In Javascript?

In this function, when it is compared the lenght of the array it is used != operator and when it is comparing all elements of the array it is using !==

Why do we prefer === and !== Over == and != In Javascript?

The == and === operators are used to check the equality of two operands. The != and !== operators are used to check the inequality of two operands.

Is != The same as ==?

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

Should I use != Or !==?

Inequality Operators: !=: Converts values if variables are different types before checking for inequality. !== : Checks both type and value for the two variables being compared.


1 Answers

It is wrong to say that != does only compare the value. Because it rather compares the types, applies a type conversion if necessary (see table of Comparison with Various Types) and then compares the (converted) values.

In contrast to that, !== fails earlier if the types are not equal and no type conversion is done.

like image 194
Gumbo Avatar answered Sep 25 '22 05:09

Gumbo