Ifempty in expression

n8n Version 1.32.1

I try to use the ifempty function within an expression but the output is not what I would have expected. I need the value as output but it seems to give me ifempty as well…

Can you tell me what am I’m doing wrong?

This is my workflow

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

I looks like you might be coming from the spreadsheet world. If not i’m curious where that syntax is from

javascript only runs inside of the {{ }} in n8n so your entire expression needs to be inside. Also, methods such as .isEmpty() go after the variable.

So you would get a true or false from .isEmpty() like this: {{ $json.code.isEmpty() }}

If you want to get a value based on that, you can use a ternarry operator like this

{{ $json.code.isEmpty() ? "value if true" : "Value if false" }}


What’s your use case here? If you want to set a default value if “code” doesn’t exist, you can use a much more concise syntax like this {{ $json.code || "default value" }}. The || means or in javascript, if the value to the left is undefined (or falsey in JS), then it will use the value to the right.

Let me know if that answers your question

Hey Liam,

I’m coming from Integromat/make and now learning to think differnt which is not that easy if you build like that for several years :slight_smile:

What I want is if no data get found with the value from the trigger then It goes to the loop and get another value. So in the set after the merge I want todo if value from loop set is empty then take the value from trigger. Maybe there is also a better way where I can do this but this is what I thought should work.

The workflow had a little wrong in the ifempty since I took first trigger then set. So it should be like this ifEmpty({{ $json.set2 }},{{ $json.code }})

I got it to work but with different code

{{ $json.set2 !== undefined && $json.set2 !== “” ? $json.set2 : $json.code }}

But I think I did your code wrong :smiling_face: yours should have worked too

That definitely works but you can simplify it to:
{{ $json.set2 ? $json.set2 : $json.code }}
or simply it even further with {{ $json.set2 || $json.code }}.

If the code I quoted worked for you then so will both of these if you just copy and paste them. Read more about it by looking up truthy and falsy values, ternary operation, and logical or operator (||)

The $json.set2 ? will automatically return false if it is undefined or “”, so you only need to put the value itself in this case

2 Likes

Thank you liam. This helped a lot to understand better!

You’re welcome!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.