I'm facing a if condition issue in javascript. Please have look on below code.
1.
const a = 1;
if(a && a > -1)
{
console.log('suceess')
}
else
{
console.log('failed')
}
Here in if condition it is returning true.
2.
const a = 0;
if(a && a > -1)
{
console.log('suceess')
}
else
{
console.log('failed')
}
Here in if condition it is returning Zero.
I'm not getting this. Can someone please explain why is it so.
a? what result do you expect?if(a && a > -1)the first check becomes false. It's just like havingif (false &&...and the second part of the if isn't even checked0is a falsey value and will be evaluated as false rendering the whole condition false. (a > -1never gets evaluated due to short-circuit evaluation)