You are currently viewing Overriding Function in JavaScript | Explained

Overriding Function in JavaScript | Explained

Overriding in JavaScript

In this article, we will see how to overriding a function in javascript. JS supports overriding but not overloading. Overring means a parent and child function have the same function name when we call the function name it returns the overriding function.

Example:

function AddNum(a, b, c) {
return a + b * c;
}

function AddNum(a, b) { // overriding function
return a + b;
}
var result = AddNum(1, 2, 3);
document.write(result);

Output:
3

See the above code, it returns the value of addition will be equal to 3 instead of 6.

In this case, we have created two functions with the same function name “AddNum”. The parent function has three parameters (a, b, c) and it returns the addition of three values. And child function has two parameters (a, b) and it returns the addition of two values. Finally, we call the function with three parameters, it executes overridden function. So, it returns 3.

Leave a Reply