question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"falsey" conditional {?.x} is not treating 0 as false

See original GitHub issue

As 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:closed
  • Created 11 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
vybscommented, Oct 17, 2012

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);

0reactions
dominykascommented, Mar 29, 2013

“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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found