IT Questions and Answers :)

Thursday, November 23, 2017

In JavaScript, what is the result of the following: (true + false) > (2 + true);

In JavaScript, what is the result of the following: (true + false) > (2 + true);

  • false
  • TypeError
  • NaN
  • true 

 
In JavaScript, what is the result of the following: (true + false) > (2 + true);

EXPLANATION

The above line of JavaScript returns false. Why? The reason is called type coercion. You're using two boolean values in an arithmetic operation, which is not possible unless the interpreter converts them into numbers first. So consider the following:
If you evaluate the statements, you find that true equals 1 and false equals 0. So your expression is equivalent to:
(1 + 0) > 2 + 1
which reduces to:
1 > 3

which is false!

Share:

0 comments:

Post a Comment

Popular Posts