function overloading in javascript

by Biswa R

JavaScript

//Function overloading is not permited in JS
//However you can write your own function to achive the same thing
//If function overloading is done in JS it won't throw any compilation error
//
function Add(x,y,z){
	console.log(x+y+z);
}
function Add(x,y){
	console.log(x+y);
}
Add(1,2,3,4);

function AddStrings(x, y, z)
{
  var result = x;
  if(typeof y !== "undefined") {result += y;}
  if(typeof z !== "undefined") {result += z;}
  console.log(result);
};

AddStrings(1,2,3,4);
AddStrings(1);