var text = 'abc';
if(text = '' || text != '')
console.log(text);
else
console.log('in else');
It is just a useless code snippet, but it gives the strange result which I was not expecting at all. So my curiosity brought me here.
It prints true
only.
Why does it updates the text
value to true
rather than setting it as empty?
The expression
text = '' || text != ''
is parsed as
text = ('' || text != '')
The value of
('' || text != '')
is the boolean value true
because text != ''
is true
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With