"falsey" conditional {?.x} is not treating 0 as false
See original GitHub issueAs per https://github.com/linkedin/dustjs/pull/127#issuecomment-9510588, heavily simplified
Using the tool with this for the template:
{?.key}
a
{:else}
b
{/.key}
And this for the JSON:
{"key": 0}
Output I’m getting:
a
Output I’m expecting to get:
b
Occurring the same in both of the following tools: http://akdubya.github.com/dustjs/ http://linkedin.github.com/dustjs/test/test.html
For reference, I have tested in Safari on OS X and Chrome on Windows.
The following “falsey” values give the correct response - b:
{}
{"key": null}
{"key": false}
{"key": ""}
The integer value 0 (in Javascript) is also considered as “falsey”, as per the following:
(0 == false) returns true
Whether intentional or not, I feel this is a bug because zero is a “falsey” value.
Issue Analytics
- State:
- Created 11 years ago
- Comments:14 (7 by maintainers)
Top Results From Across the Web
In JavaScript, why is "0" equal to false, but when tested by 'if' it ...
Now, to explain why "0" == false you should read the equality operator, which states it gets its value from the abstract operation...
Read more >Truthy and Falsy Values: When All is Not Equal in JavaScript
Anything in JavaScript can be considered truthy or falsy. Learn what these values are and the rules that apply when they're compared.
Read more >Why “0” is not equal to false in if condition in JavaScript
The reason behind this behavior is that JavaScript treats non-empty string as true. First, “0” is converted into its boolean value, by automatic ......
Read more >Falsy Values in JavaScript - freeCodeCamp
A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: ......
Read more >If Statements, Function Returns, Truthy and Falsy - Wes Bos
A string of "0" is tricky because it's a string with content, but it is also zero and we learned that zero is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Chunk.prototype.exists = function(elem, context, bodies) { var body = bodies.block, skip = bodies[‘else’];
if (!dust.isEmpty(elem)) { if (body) return body(this, context); } else if (skip) { return skip(this, context); } return this; };
it is the isEmpty method.
dust.isEmpty = function(value) { if (dust.isArray(value) && !value.length) return true; if (value === 0) return false; return (!value);
“numeric 0 evaluates to true” <-- I do not think this is intuitive. What makes this hurt even more is that there are no keywords for true/false that we could pass as parameters - meaning that the only universal falsy value (other than non-existence/undefined) is “” (empty string).