Javascript Method Overloading
In this article, we will see about the javascript method overloading. In javascript, the method is also known as function. A question for many people is, Is function overloading possible in JavaScript? like, haven’t C sharp and other programming languages. We can overload the matter just by giving different parameters and different types of parameters, so the question is we can do it in c-sharp can we do it in javascript? So, let’s try the same method.
Example :
function Add(a, b, c, d){
document.write("Method with 4 parameter <br/>");
}
function Add(a, b){
document.write("Method with 2 parameter <br/>");
}
function Add(a, b, c){
document.write("Method with 3 parameter <br/>");
}
Add(10, 20);
Add(10, 20, 30);
Add(10, 20, 30, 40);
Output:
- See the above code, we have created three functions with the same name and different parameters.
- Then we called the function “Add()” by passing different arguments.
- If we pass two arguments then the function with two parameters should be run, if we pass three arguments then the function with three parameters should be run and if we pass four arguments then the function with four parameters should be run.
- So, here executed only the three-parameter function. Because That is the last thing we have created. So no matter how many times we call it, it will run
Conclusion
Therefore, JavaScript cannot support method overloading.