What is function overloading in JavaScript?

JavaScript does not support function overloading like other programming languages such as Java or C++. When you define multiple functions with the same name, JavaScript keeps only the last defined function.

What Happens with Same Function Names

Let's see what happens when we define multiple functions with the same name:

function funcONE(x, y) {
   return x * y;
}

function funcONE(z) {
   return z;
}

// Only the last function definition is kept
console.log(funcONE(5));      // prints 5
console.log(funcONE(5, 6));   // prints 5, not 30
5
5

The second function definition overwrites the first one completely. JavaScript ignores extra parameters when calling the function.

Why This Happens

In JavaScript, function names are just variables that hold function objects. When you declare a function with the same name twice, you're simply reassigning the variable to a new function, replacing the previous one.

console.log(funcONE.length); // Shows number of parameters: 1
// The function with 2 parameters is completely lost
1

Alternative Approaches

While JavaScript doesn't have native function overloading, you can achieve similar behavior by checking the number or type of arguments:

function calculate(x, y) {
    if (arguments.length === 1) {
        return x; // Return single value
    } else if (arguments.length === 2) {
        return x * y; // Multiply two values
    }
}

console.log(calculate(5));      // 5
console.log(calculate(5, 6));   // 30
5
30

Conclusion

JavaScript doesn't support function overloading natively. The last function definition with the same name overwrites previous ones. Use parameter checking or different function names to achieve similar functionality.

Updated on: 2026-03-15T23:18:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements