Some Javascript and PHP tricky question & answers

8 + true == 9

In JavaScript, when the plus operator is placed between a number and a boolean, the boolean is converted to a number.

Like false == 0 and true == 1. So 8 + true is converted to 8 + 1 and thus we get the answer of 9.

true + false = 1

These above 2 operations will give same result in PHP and other languages as well.

'8' + 8 = 88

true + '7' = true7

1 + 1 + '1' = 21

-'60' + 60 = 0

What if we attempt to negate a string and then add a number? As you should know by now, without the negation, our answer would be '6060'. However, the negation changes things.

The minus sign before '60' is a unary minus operator that will actually convert the string to a number and make it negative. Thus, our equation becomes -60 + 60 which equals 0.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *