Optional parameter in functions in LESS

Parameters in functions can be optional. To do this, they need to be given a default value. In this case, when calling the function, we can omit the parameter value - and the specified default value will be taken. Let's look at an example.

Let's say we have a function that sets the color of an element:

styles.less.color(@c) {
  color: @c;
}

Let's make the color parameter optional and have the default value red:

styles.less.color(@c: red) {
  color: @c;
}

Let's now call our function with a parameter:

styles.lessdiv {
  .color(green);
}

Now let's call our function without a parameter:

styles.lessdiv {
  .color();
}