Function Overloading in JavaScript
Published in
2 min readFeb 17, 2021
Function Overloading In Programming Languages
Function overloading means creating multiple functions with different implementations. The rules in function overloading are
- The same function name is used for more than one function definition.
- The functions must differ by the types of their parameters.
Function Overloading in JavaScript
JavaScript does not support function overloading.
/**
* Display Name
* @param {string} name Name
*/
function displayName(name) {
return `Hi ${name}`;
}/**
* Display Name
* @param {string} fName First Name
* @param {string} lName Last Name
* The above function will be overwritten by this function.
*/
function displayName(fName, lName) {
return `Hi ${fName} ${lName}`;
}displayName('Ana'); // 'Hi Ana undefined'
In the above code example, the second function will override the first function. This is because function declarations are hoisted in JavaScript.
Function declarations in JavaScript are hoisted to the top of the enclosing function or global scope.