In most of the OOP languages like C#, we have a concept of function overloading, and we can achieve it with the same function name with different signatures and arguments.
But we don't have function signature in JavaScript. Let us take a look at what happens when we define two functions with the same name and different parameters.
- <script>
- function Num(number){
- document.write('You have passed '+number+"<br/>");
- }
- function Num(){
- document.write("You haven't passed any arguments<br/>");
- }
- Num();//Calling function with no parameters
- Num(1);//Calling function with one parameters
- </script>
Output
You haven't passed any arguments
In JavaScript, when you define multiple functions with the same name, the one that appears last will replace the earlier function declarations. This is called "function hosting". Now, let us take a look at using function objects,
- <script>
- var Num = new Function("number","document.write(\"You have passed <br/>\"+number)");
- Num = new Function("document.write(\"You haven't passed any arguments<br/>\")");
- Num();//Calling function with no parameters
- Num(1);//Calling function with one parameters
- </script>
Output
You haven't passed any argument
As you can see, the function object is being assigned to Num two times, so the first function object will be replaced.

Join the conversation! Your thoughts help the community grow.