Signup/Sign In
PUBLISHED ON: FEBRUARY 22, 2023

JavaScript Program to Perform Function Overloading

This tutorial will teach how to make a JavaScript Program to Perform Function Overloading. To understand this article we need to have a decent knowledge of JavaScript if... else Statement, switch Statement and Functions and Function Expressions. It's an easy job that can be done with a few lines of code. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

What is Function Overloading

Function Overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. However, in JavaScript, if there are multiple functions with the same name, the function that is defined at the last gets executed.

1. Using if/else-if Statement

In the program given below, the overloading feature is accomplished by using the if/else...if statement.

  • In JavaScript, the arguments object is automatically available inside a function that represents the passed arguments to a function.
  • The multiple conditions are addressed to perform actions based on that particular condition.
  • Function declarations in JavaScript are hoisted to the top of the enclosing function or global scope.

Program to Perform Function Overloading Using if/else-if Statement

// JavaScript Program to perform Function Overloading

function sum() {  
         
    if (arguments.length == 0) 
        console.log('You have not passed any argument');      

    else if (arguments.length == 1)
        console.log('Pass at least two arguments');  

    else {
        let res = 0;
   
        for (i = 0; i < arguments.length; i++) 
            res += arguments[i];  
        console.log(res); 
    }  
}

sum();
sum(1); 
sum(1, 2);    
sum(1, 2, 3, 4, 5, 6, 7, 8, 9); 


You have not passed any argument
Pass at least two arguments
3
45

2. Using switch Statement

In the program given below, the switch statement is used to accomplish the function overloading functionality. Different conditions result in different actions being performed.

Program to Perform Function Overloading Using switch Statement

// JavaScript Program to perform function overloading

function sum() {
    switch (arguments.length) {
    case 0:
        console.log('You have not passed any argument');
        break;
    case 1:
        console.log('Pass at least two arguments');
        break;
    default:
        let res = 0;
    
        for (i = 0; i < arguments.length; i++)  
            res += arguments[i];  
        
        console.log(res);
        break;
    }
}

sum();
sum(5); 
sum(1, 2);    
sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);


You have not passed any argument
Pass at least two arguments
3
55

Conclusion

In conclusion, we've explored the importance of function overloading in JavaScript and how it can improve the efficiency of your code. By creating multiple functions with the same name but different parameter types, we can simplify our code and avoid unnecessary repetition.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.