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

Maximilian Travis

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.

jfriend00

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I change the value of any global variable inside of a function?

From Dev

How can I get the value from this variable inside function?

From Dev

how can I change values inside a variable?

From Dev

How can I change user inside function

From Dev

Change the value of a variable inside a function

From Dev

node js - how do I return a value from a function when it depends on asynchronous function inside

From Dev

How can I write a function that will set the value of a variable inside a parent object?

From Dev

How can I get the value of data attribute from a function in WordPress and put it inside a variable in jquery?

From Dev

How to change value inside function

From Dev

How can I change the value of a fields inside a collection that is part of an object

From Dev

How can I change the value inside the data method

From Dev

How can I include a variable inside a callback function?

From Dev

How can I find out the name of a variable inside a function?

From Dev

How can I find out the name of a variable inside a function?

From Dev

How can I set the value of a variable inside a ng-repeat?

From Dev

How can I scrape inside a div tag with node.js?

From Dev

How do I change a variable inside a variable?

From Dev

How can i get value of Javascript function, inside jsp?

From Dev

How can i get value of Javascript function, inside jsp?

From Dev

How do I globally change a variable value within function in lisp

From Dev

How can I change the value of an enum variable in a different class?

From Dev

How can I convert a string to a variable name in Node.js?

From Dev

How can I check if an environment variable is set in Node.js?

From Dev

How can I manage Node.js async variable scope?

From Dev

How can I duplicate this JS variable and change the text used?

From Dev

How to change a variable declare inside a function in javascript

From Dev

How can I return a value from a parent function in Node?

From Dev

How can I return a value from a parent function in Node?

From Dev

How can I change the Double value in my dictionary outside the function?

Related Related

  1. 1

    How do I change the value of any global variable inside of a function?

  2. 2

    How can I get the value from this variable inside function?

  3. 3

    how can I change values inside a variable?

  4. 4

    How can I change user inside function

  5. 5

    Change the value of a variable inside a function

  6. 6

    node js - how do I return a value from a function when it depends on asynchronous function inside

  7. 7

    How can I write a function that will set the value of a variable inside a parent object?

  8. 8

    How can I get the value of data attribute from a function in WordPress and put it inside a variable in jquery?

  9. 9

    How to change value inside function

  10. 10

    How can I change the value of a fields inside a collection that is part of an object

  11. 11

    How can I change the value inside the data method

  12. 12

    How can I include a variable inside a callback function?

  13. 13

    How can I find out the name of a variable inside a function?

  14. 14

    How can I find out the name of a variable inside a function?

  15. 15

    How can I set the value of a variable inside a ng-repeat?

  16. 16

    How can I scrape inside a div tag with node.js?

  17. 17

    How do I change a variable inside a variable?

  18. 18

    How can i get value of Javascript function, inside jsp?

  19. 19

    How can i get value of Javascript function, inside jsp?

  20. 20

    How do I globally change a variable value within function in lisp

  21. 21

    How can I change the value of an enum variable in a different class?

  22. 22

    How can I convert a string to a variable name in Node.js?

  23. 23

    How can I check if an environment variable is set in Node.js?

  24. 24

    How can I manage Node.js async variable scope?

  25. 25

    How can I duplicate this JS variable and change the text used?

  26. 26

    How to change a variable declare inside a function in javascript

  27. 27

    How can I return a value from a parent function in Node?

  28. 28

    How can I return a value from a parent function in Node?

  29. 29

    How can I change the Double value in my dictionary outside the function?

HotTag

Archive