简体   繁体   中英

How can i change the value of a variable inside a function in node.js?

Im trying to use a function to change the value of a variable in node js. What i am working with happen to be async functions but i get the same behavior in regular (non async) functions as well.

Code is

async function change_string(new_string){

new_string='goodbye';

}

async function main() {
    var new_string; 
    new_string='hello';
await change_string(new_string);
console.log(new_string);

}

main();

I would like the console to log "goodbye" but it is logging "hello". There must be something about the way that javscript functions work that i do not understand. Do i need to specifically return new_string? Thank you.

First off, what you're asking about is called "side effect programming" where you call a function and it changes some variable outside the function. This is generally NOT a good way to program for a variety of good reasons. And, if you're doing it in concert with asynchronous operations, then it's typically a disaster because the timing of when things outside your function change and when others can use the new value is completely unknown and unpredictable. So, my first recommendation is "don't program this way".

Functions should either return a value or promise that the caller can use. Or, you can create an object with methods and the methods can modify the state of that particular object.

If you have asynchronous operations, then you should be returning a promise so the caller knows when the asynchronous operation is complete or had an error. If there's a "final result" from the asynchronous operation, it should be the resolved value of the promise you return.

And, I hope you realize that neither of these functions needs to be async as they don't contain any asynchronous operations.

Then, lastly, when you pass a string to a function as an argument and the function changes that string, it just changes the argument, it does not change the original variable you passed. That's how Javascript works for passing strings or any primitives as function arguments.


If you were going to create a side effect function, then you need to declare the variable that it's going to change in some higher scope that both this function and whoever else wants to use that variable can then access it. Here's an example:

 let new_string = ""; async function change_string(){ new_string = 'goodbye'; } async function main() { new_string='hello'; await change_string(); console.log(new_string); } main();

I've only coded it this way because this is what you were trying to do. This is not a good way to code any of this. Since this is a makeup set of code, I don't know what real problem you're trying to solve so we can't offer a good way to solve that problem.


I would much rather see you create a function that returns the modified string (and remove the unnecessary async designations):

 // return modified string function change_string(existing_string){ return 'goodbye ' + existing_string; } function main() { let my_string = 'hello'; let new_string = change_string(my_string); console.log(new_string); } main();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM