> For the complete documentation index, see [llms.txt](https://choubey.gitbook.io/javascript-interview-questions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://choubey.gitbook.io/javascript-interview-questions/question-87.md).

# Question 87

### Question

Describe the concept of function overloading in JavaScript.

### Answer

**Function Overloading (in Theory):**

In languages like C++, Java, or C#, function overloading allows you to define multiple functions with the same name but different parameter lists. The compiler then chooses the correct function to call based on the types and number of arguments passed during invocation.

**JavaScript's Approach:**

JavaScript doesn't have true function overloading in the traditional sense.

* **Dynamic Typing:** JavaScript is dynamically typed, meaning you don't declare variable types explicitly. This flexibility makes it harder for the language to determine which function should be called based on argument types alone.
* **Parameter Flexibility:** You can achieve some semblance of "overloading" by using:

  1. **Function Arguments as Parameters:**

  ````
  ```javascript
  ````

  ````
  function greet(name) { 
      console.log("Hello, " + name + "!"); 
  }

  greet("Alice"); // Outputs "Hello, Alice!"
  greet(123);    // Outputs "Hello, 123!" (JavaScript automatically converts the number to a string)
  ```
  ````

  2\. **Default Parameters:** You can specify default values for function parameters, allowing different behavior based on provided arguments:

  ```javascript
  function add(a = 0, b = 0) {
      return a + b;
  }

  add(5); // Returns 5 (b defaults to 0)
  add(2, 3); // Returns 5 
  ```

**Alternatives in JavaScript:**

* **Duck Typing:** Embrace the dynamic nature of JavaScript. Focus on whether an object "walks like a duck" and "quacks like a duck," rather than relying on strict type matching.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://choubey.gitbook.io/javascript-interview-questions/question-87.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.
