# Form Calculations

### Introduction

Form fields can be computed using formulas. These formulas may reference other fields in your form using variables. Formulas are written in JavaScript and executed by the browser when filling out the form. You can create formulas in the Form Builder.  See the Video Introduction below for a walkthrough on defining variables and creating your first calculation.

Note: For formulas to function properly, JavaScript must be enabled in the user's browser.
Calculations can only be used in text fields.

### Step 1: Define your Variables

A variable is a value that can be defined by an answer given in your form. A variable must be assigned to a question before it can be used in a formula. To define a variable, follow these steps:

1. Open a form in the Form Builder.
2. Click an existing question and use the Options button to open the editing sidebar.
3. Expand the Calculations tab. If you do not see a Calculations option, check the box to show advanced options.
4. Check the This is a variable box.
5. Enter a variable name for the question. Variable names may not contain space or non-alphanumeric characters.  Please see the note below under "Create a Formula" for additional details on how to add a space if needed.
6. If you selected a multiple-choice question, you must provide a value for each choice.

### Step 2: Create a Formula

A formula is evaluated as the form is filled out, and the form is submitted with the computed value.

Form formulas are single-line JavaScript equations. They are mostly used to perform simple arithmetic, such as computing a total in an order form.

Please Note:  We are unable to provide support for custom JavaScript.

To create a computed field:

1. Open a form in the Form Builder.
2. Click an existing question and use the Options button to open the editing sidebar.
3. Expand the Calculations tab. If you do not see a Calculations option, check the box to show advanced options.
4. Check the box for This is a calculated field.
5. Enter an equation into the Formula text field.  You can use any variables you have defined from step 1 above.
Note: You can concatenate a string of variables by using "\u0020" to represent a space. For example, variableONE+“\u0020”+variableTWO+“\u0020”+variableTHREE

You can also add spaces to a variable value by adding \u0020 in the value. For example: "Variable\u0020One" Will be rendered as "Variable One"

### Video Introduction

The following video is meant to provide you with an introduction to defining variables and using those variables to create a calculation. You will find additional examples of common calculations below the video tutorial.

### Examples

#### Simple formula performing arithmetic

`(a + b) / c * d`

#### 90% of the variable A, rounded to two decimals

`(A*0.9).toFixed(2)`

#### Calculating a Body Mass Index (BMI)

```height?((weight/(height*height))*703):0
// This example prevents a division by zero error by testing
// the height value first. It returns 0 if it's undefined or zero.```

#### Sample If/Else Calculation

`if(variableA=="Selection A"){variableA}else{variableB};`

#### Sample If/Else/Else If Calculation

`if(variableA=="Selection A"){variableA}else{if(variableA=="Selection B"){variableB}else{variableC}};`

### Tutorials

There are many examples when you may want to have your respondents enter both a billing and a shipping address when submitting a form. Sometimes these two addresses may be the same, and other times they may be different.

You can use form calculations to improve this process so that users will not need to enter their address twice if they select that their billing and shipping addresses are the same.

To begin you should first look at our "Shipping and Billing" Predefined Content to see if it is a good fit for your needs. This section can be instantly added to your form and will ask users to only enter their billing information if it is different than their shipping information. More details are discussed in the video below. If you prefer to have a setup where your respondents can select that their addresses are the same, and have that address information carried over into the other section, this is possible through form calculations. To set this up, please follow the steps in this video:

### FAQ

##### I'm using Zip Codes in calculations, but the leading zero keeps getting stripped. How can I keep it at 5 digits?
`if(zip){('00000'+zip).substr(-5);}else{""}`