I am confusing with following outputs.
> console.log('0&&1')
0&&1 // ok
> console.log(0&&1)
0 //ok
But when i console this below expression then it give 1. So please help me to understand this concept
> console.log('0&&1'+0&&1)
1
I am confusing with following outputs.
> console.log('0&&1')
0&&1 // ok
> console.log(0&&1)
0 //ok
But when i console this below expression then it give 1. So please help me to understand this concept
> console.log('0&&1'+0&&1)
1
+
has higher precedence than &&
. So your last snippet is essentially equivalent to:
console.log(('0&&1' + 0) && 1)
which will become this:
console.log('0&&10' && 1)
Since a non-empty string is a truthy value, thus, the return value is 1.