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.

"if" should consider an empty array to be false

See original GitHub issue

nunjucks currently considers an empty array to be true. It is common practice in jinja and its other derivatives to test for an empty array by just writing {% if arrayname %}.

From the jinja documentation:

The if statement in Jinja is comparable with the if statements of Python. In the simplest form you can use it to test if a variable is defined, not empty or not false:

{% if users %}
<ul>
{% for user in users %}
    <li>{{ user.username|e }}</li>
{% endfor %}
</ul>
{% endif %}

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
mbylstracommented, May 29, 2014

Yes, this one had me scratching my head. What is the workaround? update: this worked for me {% if users|length %}

1reaction
devoidfurycommented, May 29, 2014

In my opinion, this falls into one of the side-effects of Javascript vs Python.

In python:

not not []
=> False

And Javascript:

!![]
=> True

So I think {% if arr.length %} is more Javascript-y, and the current logic is valid. Just my opinion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do empty JavaScript arrays evaluate to true in conditional ...
arrays are objects, objects are truthy. just ask for array.length, if not zero, it will be truthy. when you explicitly convert to Boolean,...
Read more >
In JS, is an empty Array return false or true? - holy.kiwi
Because Array is type of object , the fact that an empty Array is conversed to true is correct. · But in Loose...
Read more >
How to Check if a JavaScript Array is Empty or Not with .length
To check if an array is empty or not, you can use the .length property. The length property sets or returns the number...
Read more >
How to check if an array is empty using Javascript? - Flexiple
First we check if a variable is an array using the Array. · If the variable passed is an array then the condition...
Read more >
Why is [] (empty Array or list) falsey in Python but, truthy in ...
[1] Here are most of the built-in objects considered false: constants defined to be false: None and False. zero of any numeric type:...
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