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.
in Javascript 0 is considered a "falsy" value, which mean il will be evaluate to false when used in a condition like if (0) {}.
See https://developer.mozilla.org/en-US/docs/Glossary/Falsy for more information about falsy values.