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

89,204

Solution 1

You cannot pass explicitly by reference in JavaScript; however if b were an object, and in your function you modified b's property, the change will be reflected in the calling scope.

If you need to do this with primitives you need to return the new value:

function increaseB(b) {
  // Or in one line, return b + 1;
  var c = b + 1;
  return c;
}

var b = 3;
console.log('Original:', b);
b = increaseB(b); // 4
console.log('Modified:', b);

Solution 2

You cannot change the value of an integer or a string in JavaScript BUT you can change the value of the attributes of an object for example

function change(x) {
  x.x = 2;
}

let y = { x: 1 };
console.log('Original:', y);

change(y); // will make y.x = 2;

console.log('Modified:',y);

Solution 3

When passing an object, you are passing a reference to that object. So when modifying an object that was passed into a function you are modifying that original object. Therefore you can create a very simple object containing a single variable that will allow you to carry the value around from one function to the next.

There is one thing that should be noted about the way you are putting these function calls into the parameter list of another function. I am not certain what the order of execution is for evaluating a parameter list. Perhaps someone who knows more about Javascript can answer that. However, not all languages will evaluate a parameter list left to right which means you may find the second function executes before the first. Again though, I'm not certain of the order of execution for Javascript so that may not be a concern.

function(a,b){
    var object_c = {
        c: 10
    };

    $('selector').toggle(

    //send object_c in to gather a value and store it in the "c" field
    function(object_c){},

    //send the same object_c object into the second function
    function(object_c){}
)}

Solution 4

If the variable is in the global scope, you can set it as follows:

function modify(name,newVal){
  window[name] = newVal;
}

Then, try this:

var foo = 10;
modify('foo','bar');
console.log(foo); // bar

Solution 5

you can, but its bad practice,

Don’t Reassign Your Function Arguments

https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/

use destructuring instead.

Share:
89,204
Ben
Author by

Ben

Updated on February 26, 2021

Comments

  • Ben
    Ben about 3 years

    I have one function with two parameters, for example function (a,b). I want to within the function, take the value of b, and replace that with c. I was thinking of something like $(b).replaceWith(c), but it didn't work out. I know I can create a new variable within the function with the new value, but is it possible to change the value of b itself? Is something like this possible. Or are the parameters set in stone?

    Let me try to explain what I am trying to do. There are three functions, one overarching function and then two functions within it which are triggered by toggle event. And I want the second function to do something to get a value and pass it on to the third function. So here would be the code

    function(a,b){
    
        $('selector').toggle(
    
            //I want this to gather a value and store it in a variable
            function(){},
    
            //and I want this to accept the variable and value from the previous function
            function(){}
        )}
    

    The only way I can think of doing this is to add a parameter c for the overarching function and then modify it with the first function and have the new value pass on to the second function.