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

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

A function accepts certain values as parameters and 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 parameters ?

function functionName(parameter1, parameter2, parameter3) {
   // Statements
}

Yes, it is possible to change the value of function parameters in JavaScript. However, the behavior depends on whether you're working with primitive values or objects. Let's explore different scenarios with examples.

Changing Primitive Values

When you modify primitive values (numbers, strings, booleans) inside a function, the changes don't affect the original variable outside the function.

<!DOCTYPE html>
<html>
<body>
   <script>
      function increaseValue(num) {
         num = num + 10;
         document.write('Inside function: ' + num + '<br>');
         return num;
      }
      
      var originalValue = 5;
      document.write('Original value: ' + originalValue + '<br>');
      var result = increaseValue(originalValue);
      document.write('After function call: ' + originalValue + '<br>');
      document.write('Returned value: ' + result);
   </script>
</body>
</html>
Original value: 5
Inside function: 15
After function call: 5
Returned value: 15

Modifying Object Properties

When you pass an object to a function, you can modify its properties because objects are passed by reference.

<!DOCTYPE html>
<html>
<body>
   <p id="output"></p>
   <script>
      function modifyObject(obj) {
         obj.name = "Modified Name";
         obj.age = 30;
      }
      
      let person = { name: "John", age: 25 };
      document.getElementById("output").innerHTML = 
         'Original: ' + JSON.stringify(person) + '<br>';
      
      modifyObject(person);
      document.getElementById("output").innerHTML += 
         'After modification: ' + JSON.stringify(person);
   </script>
</body>
</html>
Original: {"name":"John","age":25}
After modification: {"name":"Modified Name","age":30}

String Parameter Example

This example demonstrates that string parameters cannot be modified outside their function scope.

<!DOCTYPE html>
<html>
<body>
   <script>
      var myText = "Hello";
      
      function changeText(textParam) {
         textParam = "Welcome";
         document.write('Inside function: ' + textParam + '<br>');
      }
      
      document.write('Before function call: ' + myText + '<br>');
      changeText(myText);
      document.write('After function call: ' + myText);
   </script>
</body>
</html>
Before function call: Hello
Inside function: Welcome
After function call: Hello

Key Points

Data Type Passed By Can Modify Original?
Primitives (number, string, boolean) Value No
Objects (including arrays) Reference Yes (properties only)

Conclusion

You can modify function parameters in JavaScript, but the effect depends on the data type. Primitive values create copies, while objects allow property modifications. Use return values for primitive changes and direct property access for objects.

Updated on: 2026-03-15T23:19:00+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements