# How To: Use Expressions

As your survey becomes more advanced, you may want to use expressions to calculate a default value or create logic. The expression feature is similar to a mathematical expression or a formula in Excel. For example, you can use them to calculate a sum, BMI, or add today's date to a survey. Expressions must follow a particular order and structure, such as:

{question1} + {question2}, ({price}*{quantity}) * (100 - {discount})

You can also use functions such as: iif(), today(), age(), min(), max(), count(), avg() and others.

## Built in Functions

Below are a listing of the functions built into the Ripple Survey Creator. Content pulled from Survey JS documentation.

### Iif

iif(condition: expression, valueIfTrue: any, valueIfFalse: any): any

Explanation:

Returns the value specified by valueIfTrue if the given condition is truthly and the value specified by valueIfFalse if the condition is falsy. The condition is an expression that can contain typical operands/operators and references to question values (as question names in curly brackets).

Example:

expression: "iif({question1} + {question2} > 20, 'high', 'low')"

Explanation:

Returns true, if all questions in the specified container (panel or page) are answered correctly (a respondent provided a valid input). This function recursively validates all questions in the container. If there is an error, the function returns false, otherwise true. In case a question's value is empty and neither validators no required status are defined for the editor, validation would pass successfully.

Example:

### isDisplayMode

isDisplayMode(): Boolean

Explanation:

Returns true if a survey is in display mode.

Example:

expression: "isDisplayMode()"

### age

age(birthDate: any): number

Explanation

Returns the age according to the date of birth passed. The passed date value (which is typically taken from the referenced question) should be defined as a valid JavaScript Date.

Example:

expression: "age({birthdate})"

### currentDate

currentDate(): Date

Explanation

Returns the current date.

Example:

expression: "currentDate()"

### today

Explanation:

Returns the current date or a date calculated using an optional parameter. The parameter specifies the number of days to be added to the current date. For example, "today()" returns the current date 0 hours, 0 minutes, "today(-1) returns yesterday's date, "today(1)" returns tomorrow's date, "today(2) returns day after tomorrow date, and so on.

Examples:

expression: "today()"

expression: "today(2)"

### getDate

getDate(questionName: expression): Date

Explanation

Returns the specified question's date value.

Example:

expression: "getDate({dateQuestionForBirthday})"

### diffDays

diffDays(dateFrom: any, dateTo: any): number

Explanation

Returns the number of days between two dates. Dates are typically specified by expressions that correspond to questions (by their names in curly brackets) containing date values.

Example:

expression: "diffDays({startDate}, {endDate}) < 7"

### sum

sum(par1: number, par2: number, ...): number

Explanation:

Returns the sum of the passed arguments.

Example:

expression: "sum({total1}, {total2})"

### max

max(par1: number, par2: number, ...): number

Explanation:

Returns the largest value from a list of the passed arguments. From v1.5.19.

Example:

expression: "max({total1}, {total2})"

### min

min(par1: number, par2: number, ...): number_

Explanation:

Returns the largest value from a list of the passed arguments. From v1.5.19.

Example:

expression: "min({total1}, {total2})"

### avg

avg(par1: number, par2: number, ...): number

Explanation

Returns the average value of the passed arguments.

Example:

expression: "avg({total1}, {total2}, {total3})"

## Example - Populating Today’s Date by Default

In this example we have a consent form that requires a participant signature and we want the current date to populate into a date field automatically.

2. Navigate to the "Properties" panel on the right
3. Under the "General" section, navigate to the "Expression(?)" field
4. Type in "today()" into the field
5. Change the "Display style" dropdown to "date"
6. Click the "Save Survey" button

When the survey is opened the date field will now automatically populate with the current date. 