If we try to define multiple functions with the same name and different arguments, then the last one will be executed:
function calculateArea(a, b) { return a * b;}function calculateArea(a) { return a * a;}console.log(calculateArea(5, 7));// 25 incorrect// executes the last functionconsole.log(calculateArea(5));// 25 correct
But we can still achieve function overloading via dynamic arguments
We need to assume the highest number of arguments
Then we check if the argument is passed or not inside the function
function CatStrings(p1, p2, p3){ var s = p1; if(typeof p2 !== "undefined") {s += p2;} if(typeof p3 !== "undefined") {s += p3;} return s;};CatStrings("one"); // result = oneCatStrings("one",2); // result = one2CatStrings("one",2,true); // result = one2true
Function overriding
When you inherit another class, we can override the parent method
In JS if you define multiple functions with the same name in the block then the last function will be called