Override a JavaScript function Last Updated : 03 Nov, 2025 Comments Improve Suggest changes Like Article Like Report In JavaScript, function overriding means redefining an existing function with a new implementation. It allows developers to change or extend the behaviour of a user-defined or built-in function during runtime.You can override both user-defined and built-in JavaScript functions.The new function replaces the old one with the same name.Useful for customizing existing logic or debugging without modifying original code.Overriding built-in functions (like alert() or parseFloat()) should be done with caution.The overridden function remains in effect for the entire scope where it’s defined.[Approach 1]: Overriding a User-Defined FunctionYou can redefine an existing function by assigning a new function with the same name.The new function replaces the old one during runtime.Useful when you want to update the behaviour of your own functions. html <body style="text-align:center;"> <h1 style="color:green;" id="h1"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on the button to override" + " the function."; var down = document.getElementById('GFG_DOWN'); function Fun() { return "This is from old function"; } down.innerHTML = Fun(); function GFG_Fun() { var newFun = Fun; Fun = function() { return "This is from overridden function"; } down.innerHTML = Fun(); } </script> </body> [Approach 2]: Overriding a Built-in FunctionJavaScript allows modifying built-in functions (like parseFloat, alert, etc.).You can assign a new implementation to these functions, but it’s not recommended for production code.Commonly done for debugging, testing, or customization. html <body style="text-align:center;"> <h1 style="color:green;" id="h1"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on the button to override" + " the function."; var down = document.getElementById('GFG_DOWN'); down.innerHTML = "Floor value of '2.345' is "; function GFG_Fun() { // Override parseFloat = function(x) { return "Floor value of '2.345' is " + Math.floor(x); } // Overriding the parseFloat function and // use it as Math.floor down.innerHTML = parseFloat(2.345); } </script> </body> Create Quiz Comment P PranchalKatiyar Follow 0 Improve P PranchalKatiyar Follow 0 Improve Article Tags : JavaScript Web Technologies javascript-functions JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like