Is it possible to change the value of the function parameter in JavaScript?


In this article we are going to find out whetehr it is possibleto change the value of the function parameter in JavaScript.

A function accepts a certain values as parameters an later while calling this function we need to pass values for these parameters as arguments. Typically a function definition contains the name of the function, parameters (optional) and the body of the function.

Syntax

Following is the syntax for function parameter

function Name(paramet1, paramet2, paramet3,paramet4) {
   // Statements
}

Yes, it is possible to change the value of the function parameter in JavaScript. increment and assign to some other variable and can return the incremented value. Let’s jump into the following example to understand more about the topic.

Example

In the following example we are running the script to change the value of the function parameter.

<!DOCTYPE html>
<html>
<body>
   <script>
      function increaseB(b) {
         var c = b + 1;
         return c;
      }
      var b = 3;
      document.write('Actual:', b);
      b = increaseB(b);
      document.write('<br>Modified:', b);
   </script>
</body>
</html>

When the program is executed, a browser window is displays, the actual and modified values on the web browser.

Example

Consider another example, where we are changing the value of the attributes of an object.

<!DOCTYPE html>
<html>
<body>
   <p id="tutorial"></p>
   <p id="tutorial1"></p>
   <script>
      function change(x) {
         x.x = 2;
      }
      let y = { x: 1 };
      document.getElementById("tutorial").innerHTML= JSON.stringify(('Original:', y)) + ":Actual Value";
      change(y);
      document.getElementById("tutorial1").innerHTML= JSON.stringify(('Modified:',y)) +":Modified Value";
   </script>
</body>
</html>

The event gets triggered when the user runs the above script, resulting the actual value to change its value, and display as a modified one.

Example

Let’s look into the another example for changing the value of the function parameter.

<!DOCTYPE html>
<html>
<body>
   <script>
      var myText = "Bye";
      changeText(myText);
      function changeText(variableToUse) {
         variableToUse = "Welcome";
         document.write(myText);
      }
   </script>
</body>
</html>

When the script gets executed, the event gets triggered and changes the actual text “welcome” to “bye” and displays it.

Example

Let’s run the below code to see the output

<!DOCTYPE html>
<html>
<body>
   <script>
      function changeTheValueOfFunctionParameter(value) {
         var incrementValue = value + 1;
         return incrementValue;
      }
      var value = 190;
      document.write('The value is:', value, '<br>');
      value = changeTheValueOfFunctionParameter(value);
      document.write('After changing the value:', value);
   </script>
</body>
</html>

The event gets triggered when the user executes the above code, and the changed value and actual value are displayed on the web-browser.

Updated on: 18-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements