Why Does "Hello" > 0 Return True

Why does hello 0 return TRUE?

Because 0 is coerced to "0". See help(">"):

 If the two arguments are atomic vectors of different types, one is
coerced to the type of the other, the (decreasing) order of
precedence being character, complex, numeric, integer, logical and
raw.

Why does (0 == 'Hello') return true in PHP?

The operators == and != do not compare the type. Therefore PHP automatically converts 'Hello' to an integer which is 0 (intval('Hello')). When not sure about the type, use the type-comparing operators === and !==. Or better be sure which type you handle at any point in your program.

Why does function sometimes return 0 and sometimes return false?

This blog post might be worth reading to understand why js handles thing this way.

The value produced by a && or || operator is not necessarily of type Boolean. The value produced will always be the value of one of the two operand expressions.

Both && and || result in the value of (exactly) one of their operands:

  • A && B returns the value A if A can be coerced into false; otherwise, it returns B.
  • A || B returns the value A if A can be coerced into true; otherwise, it returns B.

If you want the result as a boolean, you can use "not-not", like this: !!expression



Related Topics



Leave a reply



Submit