In general it doesn’t make much sense to do that, but if you really want to do the way of doing it is to pass the value by reference. More details on the subject at Memory allocation in C# - Value types and reference types.
It turns out that JS does not have a way of passing values by reference in a natural way. There are some types of data that are already passed by reference, some immutable, that does not help us because we want to change the value and some changeable, that suits us.
So what we do is we encapsulate a value in a type like this so that the value changes within the function and reflects where the variable was created. It’s not beautiful but it’s the only way to do it in JS, even for having little need.
We can use an object or a array for this since they are types by mutable reference. I will do with array:
function alterar(x) {
return x[0] += 2;
}
var x = [2];
alterar(x);
console.log(x);
console.log(x[0]);
I put in the Github for future reference.
Almost always this should be done for efficiency reasons, which is not a Javascript goal, so you should not worry about doing this if it is not something natural, the return gives the result you expect, you had already done what you need, there is no need to change the value of the variable thus, only the value was left in the variable itself:
function alterar(x) {
return x += 2;
}
var x = 2;
x = alterar(x);
console.log(x);
I put in the Github for future reference.
Placed ;
because not using it is a bad habit that caught now, there are people who like to teach how to do things that will bring problems, probably to mess with unsuspecting people.
Do not directly change external variables, as was said, works, but is not the right, is like the ;
works, but will give problem at some point.
The form held is not ideal because it is changing the value of x
place and then return this value, and this case has no reason to change the local value of x
, just make the account and return (in another case it might make sense to change the x
.
Note that the x
local is not the same x
global=, even though variables have the same name is not the same variable. See more about What is the difference between scope and lifetime? and Difference between global and local scope. When you do not set a parameter, the x
is considered global, there is only it, and creates confusion, so do not.
If you have control over the version you are using it would be even better to use let
than var
that does not allow the global to be confused with the local. Better still to use Typescript. The let
does not work in older versions (which is true in some rare browsers out there). Node programming/Deno is different from doing JS in a browser.
I wanted to show without changing the content of its function, but it is better to do the following:
function alterar(x) {
return x + 2;
}
var x = 2;
x = alterar(x);
console.log(x);
I put in the Github for future reference.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero