The Wayback Machine - https://web.archive.org/web/20240603103026/https://www.geeksforgeeks.org/how-to-override-a-javascript-function/
Open In App

How to override a JavaScript function ?

Last Updated : 26 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are given an HTML document and the task is to override the function, either a predefined function or a user-defined function using JavaScript. 

Approach: When we run the script then Fun() function is called. After clicking the button the GFG_Fun() function is called and this function contains another function that will run. 

Example 1: In this example, a function written by the user is overridden. 

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>


Output:

 

Example 2: In this example, a parseFloat method is overridden using the above-mentioned approach. 

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>


Output:

 



Previous Article
Next Article

Similar Reads

How to override the current text direction using HTML ?
In this article, we will override the current text direction by using the dir attribute in the particular element. It is used to specify the text direction of the element content. It contains three values: ltr: It is the default value. This value represent the text in Left-to-right text direction. rtl: This value represent the text in right-to-left
1 min read
How to override functions of module in Node.js ?
Overriding means not change the original definition of any object or module but changing its behavior in the current scenario (where it has been imported or inherited). In order to implement overriding, we are going to override the function of 'url', the in-built module of nodeJS. Let us look at the steps involved in overriding a module: First of a
2 min read
How to override inline styles with external in CSS ?
In this article, we are going to learn how we can override inline styles with external CSS. Generally, we use inline CSS to override all the other styles. In some circumstances, we have to do the opposite. We have to override the inline CSS which has come from foreign sources and cannot be removed. Approach: To override the inline CSS, !important k
3 min read
Tailwind CSS 3 Classes doesn't Override Previous Classes
Tailwind CSS is basically a Utility-first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. Syntax&lt;div class="bg-blu
2 min read
How to override React Bootstrap active Tab default border styling?
React-Bootstrap provides various components with basic default styling. Sometimes, there is a need to override those basic default styles as per requirements. In this article, let us discuss overriding that default styling for the border of the active tab in the navbar. PrerequisitesReact JSReact-BootstrapSteps to create React Application and insta
3 min read
How to override nested NPM dependency versions?
In projects the packages download and used using npm are called dependency and each dependencies can have their own nested dependencies that also gets downloaded. These nested dependency creates conflicts due to the presence of multiple version of the same dependency. This will lead to issues like compatibility, security vulnerabilities, and unexpe
5 min read
How to override the CSS properties of a class using another CSS class ?
To override CSS properties with another class, use the `!important` directive in CSS, emphasizing a style's importance, overriding others. Applying a new class to an element replaces or modifies styles from its original class, allowing targeted adjustments to appearance, layout, or behavior. Table of Content Using !important propertyUsing specific
3 min read
Explore the concept of JavaScript Function Scope and different types of JavaScript Functions
JavaScript is based on functional programming. Therefore, functions are fundamental building blocks of JavaScript. So the function is called first-class citizens in JavaScript. Syntax: Define a functionfunction functionName(parameters) { // SET OF STATEMENTS } Call a functionfunctionName(arguments); The function execution stops when all the stateme
10 min read
How to Delay a JavaScript Function Call using JavaScript ?
When we want to call a function after a described amount of time, we use a delay function in JavaScript. There are several ways to delay the execution of a function. This can be useful for various purposes, such as creating animations, implementing debounce in search inputs, or simply delaying an action until a certain condition is met. Table of Co
2 min read
How to get the function name from within that function using JavaScript ?
Given a function and the task is to get the name of the function from inside the function using JavaScript. There are basically two methods to get the function name from within that function in JavaScript. These are: Using String substr() MethodUsing Function prototype name propertyGet the Function name using String substr() MethodThis method gets
2 min read