# Dynamic expression evaluation inside strings

**URL:** <https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101>\
**Category:** Questions\
**Created:** [April 14, 2025, 2:42pm UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101 "2025-04-14T14:42:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ori](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ori/32/73062_2.png) [@ori](https://community.n8n.io/u/ori)\
**Post date:** [April 14, 2025, 2:42pm UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/1 "2025-04-14T14:42:28Z")

</div>

I have a workflow which gets as input equality/inequality expressions which i want to act based upon. I wish to evaluate the value of the expression inside the ‘if’ node and act accordingly. I have seen that theres a utility function $evaluteExpression - but it does not work when i attempt to evalute the whole expression as a string.

For example:  
“if {{ $evaluteExpression(1==0) }}” will result in  
“if false”  
however, the following expression:  
“if {{ $evaluteExpression(“1==0” }}” (notice the quotes) will result in  
“if 1==0”

my expression is injected from a input parameter, therefore its necessarily a string.

I understand this is probably possible using the code block, but i would very much like to avoid it. Does anyone else have an idea?

- **n8n version:** 1.86.0
- **Database:** postgresdb

---

<div class="post-metadata">

**Author:** ![rbreen](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/rbreen/32/106993_2.png) [@rbreen](https://community.n8n.io/u/rbreen)\
**Post date:** [April 15, 2025, 2:22am UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/3 "2025-04-15T02:22:50Z")

</div>

Hi @ori ,

It sounds like you need to include only the expression in the backets in your if statment. Here’s a workflow.

Does this help?

Best,

Robert

---

<div class="post-metadata">

**Author:** ![ori](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ori/32/73062_2.png) [@ori](https://community.n8n.io/u/ori)\
**Post date:** [April 15, 2025, 6:43am UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/4 "2025-04-15T06:43:33Z")

</div>

Hey, I appreciate your reply!

Sorry if my previous explanation misled you, allow me to try to further clarify my original question.

I would like to receive from an input param an expression, such as:  
1==0  
1 \> 10  
1 \>= 9  
then, i would like to have an if statement that its true and false paths are determined based on the result of the expression.

Since i get the expression from a parameter, it is necessary a string. Now, while inserting 1==0 inside the if field will work, when done dynamically (and therefore is a string), the $evaluateExpression function will output the string ‘as-is’ as its result.

a close equivalent of the situation is to hardcodedly put an expression inside the if node, but surround it with quotations.

{{ $evaluateExpression(1==0) }} → false  
{{ $evaluteExpression(“1==0” }} → “1==0”  
{{ $evaluteExpression($json.expression}} → “1==0” (when the value of that param is “1==0”)

thanks:)

---

<div class="post-metadata">

**Author:** ![mohamed3nan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mohamed3nan/32/188347_2.png) [@mohamed3nan](https://community.n8n.io/u/mohamed3nan)\
**Post date:** [April 15, 2025, 9:34am UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/5 "2025-04-15T09:34:37Z")

</div>

Hi, @ori

I think you will need to parse the string in a Code node, or in the expression itself, depending on the complexity of the expression..

For example, this expression splits it into two numbers and evaluates:

```js
{{ $json.ex.includes('==') ? 
   Number($json.ex.split('==')[0]) == Number($json.ex.split('==')[1]) : 
   $json.ex }}

```

And this is a more comprehensive case that handles multiple operators:

```js
{{ (function() {
  const ex = $json.ex;
  const operators = ['==', '>=', '<=', '>', '<', '!='];
  
  for (const op of operators) {
    if (ex.includes(op)) {
      const [left, right] = ex.split(op).map(part => Number(part.trim()));
      
      switch(op) {
        case '==': return left == right;
        case '>=': return left >= right;
        case '<=': return left <= right;
        case '>': return left > right;
        case '<': return left < right;
        case '!=': return left != right;
      }
    }
  }
  
  return ex;
})() }}

```

here is a test:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/4/2/42d289faa0f38e1008f247b3f013d8e9130986e8.png)

---

<div class="post-metadata">

**Author:** ![Olek](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/olek/32/59619_2.png) [@Olek](https://community.n8n.io/u/Olek)\
**Post date:** [April 15, 2025, 9:50am UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/6 "2025-04-15T09:50:08Z")

</div>

Apparently, you need to embrace the expression to evaluate in its own double-handlebars.

Note the escape character in front of the “inner” closing handlebars `\}}`.

---

<div class="post-metadata">

**Author:** ![mohamed3nan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mohamed3nan/32/188347_2.png) [@mohamed3nan](https://community.n8n.io/u/mohamed3nan)\
**Post date:** [April 15, 2025, 9:54am UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/7 "2025-04-15T09:54:29Z")

</div>

Awesome @Olek , I tried that but didn’t work,

but this is the trick that makes it work,

> [@Olek](#):
>
> Note the escape character in front of the “inner” handlebars `\}}`.

👏🏻

---

<div class="post-metadata">

**Author:** ![Olek](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/olek/32/59619_2.png) [@Olek](https://community.n8n.io/u/Olek)\
**Post date:** [April 15, 2025, 10:03am UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/8 "2025-04-15T10:03:03Z")

</div>

It was a blind shot. Luckily escaping arbitrary characters doesn’t discard sequences of no special meaning.

---

<div class="post-metadata">

**Author:** ![ori](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/ori/32/73062_2.png) [@ori](https://community.n8n.io/u/ori)\
**Post date:** [April 15, 2025, 10:24am UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/9 "2025-04-15T10:24:58Z")

</div>

Thank you both so much for the help, this works!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [April 22, 2025, 10:25am UTC](https://community.n8n.io/t/dynamic-expression-evaluation-inside-strings/101101/10 "2025-04-22T10:25:37Z")

</div>

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