Custom Functions in Expressions

Edit in →

Custom functions enable you to perform any calculations in JavaScript code and use calculation results in expressions within survey JSON schemas. This demo shows how to implement, register, and use a custom function in your survey or form. In this example, a custom function counts matrix rows with a specific value. If all three rows are answered "Agree" or "Strongly agree", the survey displays a form field for positive feedback; if all three rows receive a "Disagree" or "Strongly disagree" value, a form field for negative feedback is displayed.

To implement a custom function for use in expressions, follow the steps below:

  1. Declare a JavaScript function.
    This function accepts all arguments in one array-like object. You can pass as many arguments as needed when you call the function from an expression.

    function myFunc(params) {
      let q1_value = params[0];
      let q2_value = params[1];
      // ...
      return someValue;
    }
    
  2. Register your function in FunctionFactory.
    Call a static register() method on the FunctionFactory.Instance object. This method accepts a function name that you want to use in expressions and the function itself.

    import { FunctionFactory } from "survey-core";
    
    function myFunc(params) {
      // ...
    }
    FunctionFactory.Instance.register("myFunc", myFunc);
    
  3. Use your function in expressions.
    The following code shows how to use a custom function in the visibleIf property.

    const surveyJson = {
      "elements": [{
        "type": "text",
        "name": "my-text-box",
        // ...
        "visibleIf": "myFunc({question1}, {question2}) > 0"
      }, {
        // ...
      }]
    };
    

Ensure that the custom functions you use in your survey are available in the environment where it will be run. If they are unavailable, and you cannot change the environment, consider using calculated values instead. Unlike custom functions, calculated values are embedded in a survey JSON schema and guaranteed to be available without additional JavaScript code.

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.