Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:42:13+00:00 2026-06-11T18:42:13+00:00

I am currently writing a JS rules engine which at one point needs to

  • 0

I am currently writing a JS rules engine which at one point needs to evaluate boolean expressions using the eval() function.

Firstly I construct an equation as such:

var equation = "relation.relatedTrigger.previousValue" + " " + relation.operator +
        " " + "relation.value";

relation.relatedTrigger.previousValue is the value I want to compare.

relation.operator is the operator (either “==”, “!=”, <=, “<“, “>”, >=”).

relation.value is the value I want to compare with.

I then simply pass this string to the eval function and it returns true or false as such:

return eval(equation);

This works absolutely fine (with words and numbers) or all of the operators except for >= and <=. E.g. When evaluating the equation:

relation.relatedTrigger.previousValue <= 100

It returns true when previousValue = 0,1,10,100 & all negative numbers but false for everything in between.

I would greatly appreciate the help of anyone to either answer my question or to help me find an alternative solution.

Regards,

Augier.

P.S. I don’t need a speech on the insecurities of the eval() function. Any value given to relation.relatedTrigger.previousValue is predefined.

edit: Here is the full function:

function evaluateRelation(relation)
{
console.log("Evaluating relation")
var currentValue;

//if multiple values
if(relation.value.indexOf(";") != -1)
{
    var values = relation.value.split(";");
    for (x in values)
    {

        var equation = "relation.relatedTrigger.previousValue" + " " + relation.operator +
        " " + "values[x]";
        currentValue = eval(equation);
        if (currentValue)
            return true;
    }
    return false;
}

//if single value
else
{
    //Evaluate the relation and get boolean
    var equation = "relation.relatedTrigger.previousValue" + " " + relation.operator +
        " " + "relation.value";
        console.log("relation.relatedTrigger.previousValue " + relation.relatedTrigger.previousValue);
    console.log(equation);
    return eval(equation);
}
}

Answer: Provided by KennyTM below. A string comparison doesn’t work. Converting to a numerical was needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:42:15+00:00Added an answer on June 11, 2026 at 6:42 pm

    You didn’t show how relation.relatedTrigger.previousValue is obtained, but I guess the type of this variable is still a string. In this case, the right hand side will be treated as a string instead. A string comparison matches all characteristics you mentioned:

    >>> '-213' <= '100'
    true
    >>> '0' <= '100'
    true
    >>> '1' <= '100'
    true
    >>> '2' <= '100'
    false
    >>> '10' <= '100'
    true
    >>> '13' <= '100'
    false
    

    You need to make sure relation.relatedTrigger.previousValue is a number. One solution is use the unary + operator in the comparison, e.g.

    +relation.relatedTrigger.previousValue <= 100
    

    This has nothing to do with eval. The problem is the overly liberal implicit conversion in Javascript.


    Edit: By the way, instead of eval, you could use a dictionary of functions instead. This is faster and also safer. See http://jsperf.com/eval-vs-function-map.

    var fmap = {
        '>=': function(a, b) { return a >= b; },
        ...
    };
    
    fmap[relation.operator](+relation.relatedTrigger.previousValue, 
                            +relation.value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing a program using Weka that builds a model (using one
I'm currently writing a function what would create a zip file, which will be
I'm currently writing a billing application using EF 5 Code First, and I'm running
I'm currently writing several Python modules which perform some I/O. Thoses modules can be
I am currently writing a script which parses the ServiceTage, Computername and Username from
I am currently writing rules for my pages, with .htaccess. I have a forum,
I am currently writing a Chrome extension whose basic function is to serve as
I am creating a jquery function which allows users to create rules and append
I'm writing a grammar that supports arbitrary boolean expressions. The grammar is used to
I am currently writing a Makefile that has several nearly identical rules/recipes to merge

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.