Javascript | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 2

Javascript

What would be the output of this code snippet? var a=10, b=20, c=30; if(c>b>a) {console.log('1')}; else {console.log('2')};

17th May 2020, 10:50 AM
Kajal Kumari
Kajal Kumari - avatar
2 Respuestas
+ 6
Abhay is right. the boolean evaluation works from left to right. therefore if something is true will return 1, if false will return 0, and after that, it goes to the next evaluation on the right using the returned value as comparison what actually happens is this: c > b > a means 30 > 20 > 10 the evaluation is from left to right meaning this c > b => 30 > 20 => 1 => true since c > b is 1 (true) you will continue like this: 1 > a => 1 > 10 => 0 => false the result is false
17th May 2020, 11:13 AM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 6
The output is 2 ,I have no logical reason behind it but seems like c>b evalutates to True first and is converted to 1 for True and 1>a is checked which is false
17th May 2020, 10:56 AM
Abhay
Abhay - avatar