# Spread Operator

## Description

The spread operator (`...`) in function definitions collects all passed arguments into a single array. This is useful when you want to handle a variable number of arguments in your function.

## Syntax

```javascript
def functionName(...parameterName) (
    // Inside the function, parameterName is an array containing all arguments
)
```

## Usage Examples

```javascript
// Basic example - counting arguments
def countArgs(...args) (
    return args.len
)

log countArgs(1, 2, 3)      // 3
log countArgs("a", "b")     // 2
log countArgs()             // 0

// Summing numbers
def sum(...numbers) (
    total = 0
    for i numbers.len (
        total += numbers[i]
    )
    return total
)

log sum(1, 2, 3, 4)        // 10
log sum(10, 20)            // 30

// Finding maximum value
def findMax(...values) (
    if values.len == 0 (
        return null
    )
    max = values[0]
    for i values.len (
        if values[i] > max (
            max = values[i]
        )
    )
    return max
)

log findMax(1, 5, 3, 9, 2) // 9

// Joining strings with separator
def join(...strings) (
    return strings.join(" ")
)

log join("hello", "world", "!") // "hello world !"
```

## Important Notes

* The spread parameter must be the last (and often only) parameter in the function definition
* Inside the function, the spread parameter is treated as a regular array
* If no arguments are passed, the spread parameter will be an empty array
* You can use any valid array methods on the spread parameter
* The spread operator in function definitions is different from the spread operator used to expand arrays


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://osl.mistium.com/custom-syntax/spread-operator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
