2

my question is as simple as this: How can I pass parameters as reference or as value in JavaScript as I could do in PHP.

Example with PHP:

    function increase(&$value){ 
        $value++;
        return $value; 
    }
    $number = 4;
    echo increase($number) . "<br/>";
    echo $number;

How can I accomplish something similar with JavaScript?

Thank you and Blessings.

1 Answers1

0

Assign the return value to a variable.

const nextNumber = increase(number);
console.log(nextNumber);

where increase does the same thing as your PHP function:

function increase($value){ 
    $value++;
    return $value; 
}
Snow
  • 3,820
  • 3
  • 13
  • 39
  • It's the same as in @ReynaldRamirez's code. Increment a variable and return it. – Snow Sep 15 '20 at 19:15
  • Your code doesn't work the same as in PHP. If you will run `increase` multiple times in JS it will always return the same value, but in PHP it will increase value every time. So in JS if you will pass 4 it will return always 5, but in PHP it will return 5, 6, 7, etc. – Ivan Pruchai Feb 14 '22 at 13:02