I got an issue that negation of zero produces false in my JavaScript code.
I have simplified the code to demo the issue as given below.
<input id="iid" value="0" />
<script type="text/javascript">
zero = document.getElementById('iid').value;
alert( ( !zero ? 'true' : 'false' ) ); // alert message is "false".
</script>
Why negation of zero become false?
You’re negating the string
"0". Any string becomesfalsewhen negated except the empty string:The last expression is
truebecause the+operator converts the string into a number.An input value is always a string, which makes sense semantically as well because it’s a combination of characters entered by the user. If you want to interpret is as a number you’ll have to convert it into one yourself.