A tracking transformation should be easy to explain: give it a value, apply a stated rule, and inspect the result. A function lets you keep those steps together. It also makes hidden dependencies easier to identify.
The useful question is not how many JavaScript features you know. It is whether another analyst can tell what the function accepts, what it returns, and which information it uses.
Define one transformation before writing the function
Consider a hypothetical report-export event. One application component supplies CSV, another supplies csv with surrounding spaces. The tracking specification requires the exact category csv or pdf. Any other value should remain invalid rather than becoming an invented category.
This is a small function contract: accept a string, remove surrounding whitespace, convert its letters to lowercase, and return it only when it matches the allowed formats. For non-string or unsupported input, return undefined. These are proposed rules for this example, not a claim about an existing website.
A function definition names the operation and lists its parameters. Calling the function supplies arguments and runs the operation. MDN’s functions guide explains this distinction. In our example, value is the parameter; " CSV " is one possible argument.
Return a result instead of changing the page
The following illustrative function implements that contract. It reads its argument and produces a result. It does not send an analytics event, update the data layer, or alter the original input.
function normaliseExportFormat(value) {
if (typeof value !== 'string') return undefined;
var format = value.trim().toLowerCase();
if (format === 'csv' || format === 'pdf') return format;
return undefined;
}