# IF

This function takes pairs of tests (which must be booleans after evaluation) and values (which can be any data type). It then goes through the tests in the order they were provided and outputs the value associated with whatever test is first **TRUE**. If the value of every test is **FALSE**, the IF function outputs NULL by default. The IF function also accepts as optional input a value to return in such cases; if such a value was provided, this the the value the IF function will output should none of the given tests are **TRUE**.&#x20;

**Declaration**

```javascript
IF(test_1, value_if_true_1) -> result
```

```javascript
IF(test_1, value_if_true_1, value_if_false) -> result
```

```javascript
IF(  
 test_1,   
 value_if_true_1,   
 test_2,   
 value_if_true_2) -> result
```

```javascript
IF(  
 test_1,   
 value_if_true_1,   
 test_2,   
 value_if_true_2,   
 value_if_false) -> result
```

```javascript
IF(  
 test_1,   
 value_if_true_1,   
 test_2,   
 value_if_true_2,  
 ...   
 test_n,   
 value_if_true_n) -> result
```

```javascript
IF(  
 test_1,   
 value_if_true_1,   
 test_2,   
 value_if_true_2,  
 ...  
 test_n,   
 value_if_true_n,  
 value if false) -> result
```

&#x20;

**Parameters**

**test\_1** (required, type: _boolean_) The first condition to test.

**value\_if\_true\_1** (required, type: _any_) The value to be returned if **test\_1** is **TRUE**.

**test\_2** (optional, type: _boolean_) The second condition to test. If **test\_1** is **FALSE**, **test\_2** will be evaluated; otherwise, **value\_if\_true\_1** will be returned. This parameter is optional, but if it is present, then **value\_if\_true\_2** must also be provided; this is what will be returned if **test\_2** is true.

**value\_if\_true\_2** (optional, type: _any_) The value to be returned if **test\_2** is **TRUE**(but **test\_1** is **FALSE**).

**test\_n** (optional, type: _boolean_) The _&#x6E;_&#x74;h condition to test. If all previous test parameters are **FALSE**, **test\_n** will be evaluated, and, if it is true, **value\_if\_true\_n** will be returned. The IF function supports as many pairs of tests and values as are needed, but they must be provided in pairs.

**value\_if\_true\_n** (optional, type: _any_) The value to be returned if **test\_n** is **TRUE**(but all previous tests are **FALSE**).

**value\_if\_false** (optional, type: _any_) If no tests are TRUE, the result of the value\_if\_false parameter is returned. This parameter is opitional, if no value\_if\_false parameter is provided the result will be NULL.

**Return Values**

**result** (type: _any_) When one of the test parameters is **TRUE**, this result will be the corresponding **value\_if\_true** parameter. Otherwise, the result will be the **value\_if\_false parameter** when it is provided and NULL otherwise.

**Examples**

The following example checks to see if the boolean **TRUE** is **TRUE**. It is, and thus IF outputs the associated value, which is the number 1:&#x20;

```javascript
IF(TRUE, 1) -> 1
```

The following example checks to see if the boolean **FALSE** is **TRUE**. It isn't, and because no other pairs of tests and values were provided, there is nothing else for the IF function to test:

```javascript
IF(FALSE, 1) -> NULL
```

The following example checks to see if the boolean **FALSE** is **TRUE**. It isn't, and because no other pairs of tests and values were provided, there is nothing else for the IF function to test. By default, the IF function outputs NULL if all tests are **FALSE**, but because a value was given for **value\_if\_false** ("This was false"), the function outputs that instead. Note that the values given for **value\_if\_true\_1**and **value\_if\_false** are different data types – **value\_if\_true\_1**is a number (1), and **value\_if\_false** is a string ("This was false"). The values given as potential output of the IF function can be any data type and do not at all depend on each other:

```javascript
IF(FALSE, 1, "This was false") -> "This was false"
```

The following example checks to see if 1 = 1 is **TRUE**. It is, and thus IF outputs the associated value, which is the string "The first test was true". There were other pairs of tests and values given as input, but they do not effect the outcome; once the first test was established **TRUE**, none of the others were evaluated. It makes not difference that the later tests were equally true:&#x20;

```javascript
IF(  
 1 = 1,  
 "The first test was true",  
 2 = 2,  
 "The second test was true",  
 3 = 3,   
 "The third test was true") -> "The first test was true"
```

The last example checks to see if 1 = 2 is **TRUE**. It isn't, and so the function moves on to check if 2 = 2 is **TRUE**. It is, and thus IF outputs the associated value, which is the string "The second test was true". There was another pair of tests and values given as input, but they do not effect the outcome; once the second test was established **TRUE**, the third test was not evaluated.

```javascript
IF(  
 1 = 2,  
 "The first test was true",  
 2 = 2,  
 "The second test was true",  
 3 = 3,   
 "The third test was true",  
 "No tests were true") -> "The second test was true"
```

**Discussion**

In the above examples, for the sake of clarity, all variables were hardcoded. They do not need to be; in fact, in practice they will almost never be entirely hardcoded. There is no need to to use software to check and see if an expression is **TRUE** after you just typed it.

It is not uncommon to use the IF function in tandem with other Airscript functions, particularly functions that output a boolean, such as [ISEMAIL](../email-functions/isemail), [ISEMPTY](isempty), or [ISPHONE](../phone-functions/isphone) (or other functions beginning with IS). For instance, the following example checks to see if the string "support@airkit.com" is a valid email. It is, and thus IF outputs the associated value, which is the string "This is a valid email":&#x20;

```javascript
IF(ISEMAIL(support.airkit.com), "This is a valid email") -> "This is a valid email"
```
