# JavaScript in Make

IML is a mustache-like markup language with support for evaluating complex expressions in JavaScript-like syntax. Expressions are written between mustaches: `{{body.data}}`.

## Available operators

<table><thead><tr><th width="248.4444580078125" valign="top">Operators</th><th valign="top">Examples</th></tr></thead><tbody><tr><td valign="top">Algebraic operators</td><td valign="top"><code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code></td></tr><tr><td valign="top">Equality operators</td><td valign="top"><code>==</code> (same as <code>=</code> or <code>===</code>), <code>!=</code> (same as <code>!==</code>)<br><br>All equality operations are strict.</td></tr><tr><td valign="top">Relational operators</td><td valign="top"><code>&#x3C;</code>, <code>&#x3C;=</code>, <code>></code>, <code>>=</code></td></tr><tr><td valign="top">Logial operators</td><td valign="top"><code>&#x26;&#x26;</code>, <code>||</code>, <code>!</code></td></tr></tbody></table>

## Strings inside expressions

You can use `'` or `"` to put strings inside IML expressions:

```
{{'Hello, ' + item.name}}
{{"Hello, " + item.name}}
```

## Conditionals

You can create conditionals with `if` and `ifempty` functions:

```
{{if(body.id > 100, body.user, false)}}
{{ifempty(body.username, body.user)}}
```

The `ifempty` function returns a second argument when the first argument is either not defined, null, or an empty string. Otherwise it returns the first argument.

## Retrieve collection properties

You can use normal JavaScript `.` (dot) notation to retrieve properties of collections, such as `parameters`: `{{parameters.input.data}}`.

If the property name contains spaces or starts with a non-alphabetic character or contains non-alphanumeric characters (except underscore `_`), you can use `` ` `` (back ticks) to retrieve such properties: ``{{headers.`X-HOOK-TYPE`}}`` or ``{{parameters.`First Name`}}``.

## Retrieve array items

You can use normal JavaScript `[]` bracket notation to retrieve a specific element from an array: `{{body.data[2].prop}}`. Unlike JavaScript, IML uses `1` to access the first item in an array.

You can also use `-1` to access the last element of an array: `body.data[-1].prop`.

If you need to retrieve a property of an object and the property name is contained in a variable or computed, the dot `.` notation will not work. For this, you can use the `get` function, where the first argument of the function is the object you wish to retrieve the property from, and the second argument is the property name, or a variable containing a property name, or an expression that resolves to a property name: `get(body.data, 'some property')`, `get(body, parameters.fieldName)`, `get(body.data, if(body.xml == true, 'xml', 'json'))`

## Built-in IML functions

There are many built-in IML functions.

### General functions

<table><thead><tr><th width="194" valign="top">Function</th><th valign="top">Specification</th></tr></thead><tbody><tr><td valign="top"><code>get()</code></td><td valign="top">Returns the value path of an object or array. To access nested objects, use dot notation. The first item in an array is index 1.</td></tr><tr><td valign="top"><code>if()</code></td><td valign="top">Returns value 1 if the expression is evaluated to be true. Otherwise, it returns value 2.</td></tr><tr><td valign="top"><code>ifempty()</code></td><td valign="top">Returns value 1 if this value is not empty. Otherwise, it returns value 2.</td></tr><tr><td valign="top"><code>switch()</code></td><td valign="top">Evaluates one value (called the expression) against a list of values, and returns the result corresponding to the first matching value.</td></tr><tr><td valign="top"><code>omit()</code></td><td valign="top">Removes elements with the given keys from a collection. Returns a collection with the remaining elements. Useful when you want to pass a collection to an API, which expects an exact number of elements in the collection. Using this function, you can make sure your collection meets the requirements of the API.</td></tr><tr><td valign="top"><code>pick()</code></td><td valign="top">Picks elements with the given keys from a collection. Returns a collection that contains only the picked elements. Useful when you want to pass a collection to an API, which expects an exact number of elements in the collection. Using this function, you can make sure your collection meets the requirements of the API.</td></tr></tbody></table>

### String functions

<table><thead><tr><th width="250.88885498046875">Function</th><th>Specification</th></tr></thead><tbody><tr><td><code>ascii()</code></td><td>Removes all non-ascii characters from a text string.</td></tr><tr><td><code>base64()</code></td><td>Transforms text to base64.</td></tr><tr><td><code>capitalize()</code></td><td>Converts the first character in a text string to uppercase.</td></tr><tr><td><code>contains()</code></td><td>Verifies if text contains the search string.</td></tr><tr><td><code>decodeURL()</code></td><td>Decodes special characters in URL to text.</td></tr><tr><td><code>encodeURL()</code></td><td>Encodes special characters in a text to a valid URL address.</td></tr><tr><td><code>escapeHTML()</code></td><td>Escapes all HTML tags in text.</td></tr><tr><td><code>escapeMarkdown()</code></td><td>Escapes all Markdown tags in text.</td></tr><tr><td><code>indexOf()</code></td><td>Returns the position of the first occurrence of a specified value in a string. This method returns '-1' if the value searched for never occurs.</td></tr><tr><td><code>length()</code></td><td>Returns the length of a text string (number of characters) or binary buffer (buffer size in bytes).</td></tr><tr><td><code>lower()</code></td><td>Converts all alphabetical characters in a text string to lowercase.</td></tr><tr><td><code>md5()</code></td><td>Calculates the md5 hash of a string.</td></tr><tr><td><code>mime()</code></td><td>Gets MIME Type of the file from its filename with extension.<br>E.g. calling <em>mime(picture.png)</em> will return <em>image/png</em> MIME Type.</td></tr><tr><td><code>replace()</code></td><td>Replaces the search string with the new string.</td></tr><tr><td><code>replaceEmojiCharacters()</code></td><td>Replaces emoji characters with the new string.</td></tr><tr><td><code>sha1()</code></td><td>Calculates the sha1 hash of a string. If the key argument is specified, sha1 HMAC hash is returned instead. Supported encodings: <em>hex</em> (default), <em>base64,</em> or <em>latin1</em>.</td></tr><tr><td><code>sha256()</code></td><td>Calculates the sha256 hash of a string. If the key argument is specified, sha256 HMAC hash is returned instead. Supported encodings: <em>hex</em> (default), <em>base64,</em> or <em>latin1</em>.</td></tr><tr><td><code>sha512()</code></td><td>Calculates the sha512 hash of a string. If the key argument is specified, sha512 HMAC hash is returned instead. Supported encodings: <em>hex</em> (default), <em>base64,</em> or <em>latin1</em>. Supported key encodings: <em>text</em> (default), <em>hex</em>, <em>base64,</em> or <em>binary</em>. When using <em>binary</em> key encoding, a key must be a buffer, not a string.</td></tr><tr><td><code>split()</code></td><td>Splits a string into an array of strings by separating the string into substrings.</td></tr><tr><td><code>startcase()</code></td><td>Capitalizes the first letter of every word and lower cases of all other letters.</td></tr><tr><td><code>stripHTML()</code></td><td>Removes all HTML tags from text.</td></tr><tr><td><code>substring()</code></td><td>Returns a portion of a text string between the "start" position and "the end" position.</td></tr><tr><td><code>toBinary()</code></td><td>Converts any value to binary data. You can also specify encoding as a second argument to apply binary conversions from hex or base64 to binary data.</td></tr><tr><td><code>toString()</code></td><td>Converts any value to a string.</td></tr><tr><td><code>trim()</code></td><td>Removes space characters at the start or end of the text.</td></tr><tr><td><code>upper()</code></td><td>Converts all alphabetical characters in a text string to uppercase.</td></tr></tbody></table>

### Array functions

<table><thead><tr><th width="192">Function</th><th>Specification</th></tr></thead><tbody><tr><td><code>add()</code></td><td>Adds values specified in parameters to an array and returns that array.</td></tr><tr><td><code>contains()</code></td><td>Verifies if an array contains the value.</td></tr><tr><td><code>deduplicate()</code></td><td>Removes duplicates from an array.</td></tr><tr><td><code>distinct()</code></td><td>Removes duplicates inside an array. Use the <em>key</em> argument to access properties inside complex objects. To access nested properties, use dot notation. The first item in an array is index 1.</td></tr><tr><td><code>first()</code></td><td>Returns the first element of an array.</td></tr><tr><td><code>flatten()</code></td><td>Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.</td></tr><tr><td><code>join()</code></td><td>Concatenates all the items of an array into a string, using a specified separator between each item.</td></tr><tr><td><code>keys()</code></td><td>Returns an array of a given object's or array's properties.</td></tr><tr><td><code>last()</code></td><td>Returns the last element of an array.</td></tr><tr><td><code>length()</code></td><td>Returns the number of items in an array.</td></tr><tr><td><code>map()</code></td><td>Returns a primitive array containing values of a complex array. Allows filtering values. Use raw variable names for keys.</td></tr><tr><td><code>merge()</code></td><td>Merges two or more arrays into one array.</td></tr><tr><td><code>remove()</code></td><td>Removes values specified in the parameters of an array. Effective only in the case of primitive arrays of text or numbers.</td></tr><tr><td><code>reverse()</code></td><td>The first element of the array becomes the last element and vice versa.</td></tr><tr><td><code>shuffle()</code></td><td>Shuffles (randomly reorders) elements of an array.</td></tr><tr><td><code>slice()</code></td><td>Returns a new array containing only selected items.</td></tr><tr><td><code>sort()</code></td><td>Sorts values of an array.</td></tr><tr><td><code>toArray()</code></td><td>Converts a collection into an array of key-value collections.</td></tr><tr><td><code>toCollection()</code></td><td>Converts an array containing objects with key-value pairs into a collection.</td></tr></tbody></table>

### Date & Time functions

<table><thead><tr><th width="193">Function</th><th>Specification</th></tr></thead><tbody><tr><td><code>formatDate()</code></td><td>Formats a date into a specific format, e.g. <code>'YYYY-MM-DD'</code>.</td></tr><tr><td><code>parseDate()</code></td><td>Parses a date in a specific format, e.g. <code>'YYYY-MM-DD HH:mm'</code>.</td></tr><tr><td><code>addDays()</code></td><td>Returns a new date as a result of adding a given number of days to a date. To subtract days, enter a negative number.</td></tr><tr><td><code>addHours()</code></td><td>Returns a new date as a result of adding a given number of hours to date. To subtract hours, enter a negative number.</td></tr><tr><td><code>addMinutes()</code></td><td>Returns a new date as a result of adding a given number of minutes to date. To subtract minutes, enter a negative number.</td></tr><tr><td><code>addMonths()</code></td><td>Returns a new date as a result of adding a given number of months to date. To subtract months, enter a negative number.</td></tr><tr><td><code>addSeconds()</code></td><td>Returns a new date as a result of adding a given number of seconds to date. To subtract seconds, enter a negative number.</td></tr><tr><td><code>addYears()</code></td><td>Returns a new date as a result of adding a given number of years to date. To subtract years, enter a negative number.</td></tr><tr><td><code>setSecond()</code></td><td>Returns a new date with the seconds specified in parameters. Accepts numbers from 0 to 59. If a number is given outside of this range, it will return the date with the seconds from the previous or subsequent minute(s), accordingly.</td></tr><tr><td><code>setMinute()</code></td><td>Returns a new date with the minutes specified in parameters. Accepts numbers from 0 to 59. If a number is given outside of this range, it will return the date with the minutes from the previous or subsequent hour(s), accordingly.</td></tr><tr><td><code>setHour()</code></td><td>Returns a new date with the hour specified in parameters. Accepts numbers from 0 to 59. If a number is given outside of this range, it will return the date with the hour from the previous or subsequent day(s), accordingly.</td></tr><tr><td><code>setDay()</code></td><td>Returns a new date with the day specified in parameters. It can be used to set the day of the week, with Sunday as 1 and Saturday as 7. If the given value is from 1 to 7, the resulting date will be within the current (Sunday-to-Saturday) week. If a number is given outside of the range, it will return the day from the previous or subsequent week(s), accordingly.</td></tr><tr><td><code>setDate()</code></td><td>Returns a new date with the day of the month specified in parameters. Accepts numbers from 1 to 31. If a number is given outside of the range, it will return the day from the previous or subsequent month(s), accordingly.</td></tr><tr><td><code>setMonth()</code></td><td>Returns a new date with the month specified in parameters. Accepts numbers from 1 to 12. If a number is given outside of this range, it will return the month in the previous or subsequent year(s), accordingly.</td></tr><tr><td><code>setYear()</code></td><td>Returns a new date with the year specified in parameters.</td></tr></tbody></table>

### Math functions

<table><thead><tr><th width="195">Function</th><th>Specification</th></tr></thead><tbody><tr><td><code>abs()</code></td><td>Returns the absolute value of an integer.</td></tr><tr><td><code>average()</code></td><td>Returns the average value of the numeric values in a specific array, or the average value of numerical values entered individually.</td></tr><tr><td><code>ceil()</code></td><td>Returns the smallest integer greater than or equal to a specified number.</td></tr><tr><td><code>floor()</code></td><td>Returns the largest integer less than or equal to a specified number.</td></tr><tr><td><code>formatNumber()</code></td><td>Returns a number in the requested format. A decimal point is, by default, Thousands separator is, by default.</td></tr><tr><td><code>max()</code></td><td>Returns the largest number in a specified array, or the largest number among numbers entered individually.</td></tr><tr><td><code>median()</code></td><td>Returns the median of the values in a specified array, or the median of numbers entered individually.</td></tr><tr><td><code>min()</code></td><td>Returns the smallest number in a specified array, or the smallest number among numbers entered individually.</td></tr><tr><td><code>parseNumber()</code></td><td>Parses a string with a number and returns the number.</td></tr><tr><td><code>round()</code></td><td>Rounds a numeric value to the nearest integer.</td></tr><tr><td><code>stdevP()</code></td><td>Returns the standard deviation of a specified array of population values, or the standard deviation of numbers entered individually.</td></tr><tr><td><code>sum()</code></td><td>Returns the sum of the values in a specified array, or the sum of numbers entered individually.</td></tr><tr><td><code>trunc()</code></td><td>Truncates a number to an integer by removing the fractional part of the number.</td></tr></tbody></table>

### JSON functions

<table><thead><tr><th width="195">Function</th><th>Specification</th></tr></thead><tbody><tr><td><code>createJSON()</code></td><td>Creates a JSON string inside a JSON object.</td></tr><tr><td><code>parseJSON()</code></td><td>Parses a JSON string inside a JSON object.</td></tr></tbody></table>

{% hint style="info" %}
In the custom functions, the `JSON.stringify()` or `JSON.parse()` functions should be used instead of `createJSON()` or `parseJSON()`.
{% endhint %}

{% hint style="info" %}
See also [Custom IML Functions](https://developers.make.com/custom-apps-documentation/app-components/iml-functions)
{% endhint %}
