How do I pass variables between functions in Javascript?

103,070

Solution 1

You need to either pass it between them, or it seems from your example, just declare it in a higher scope:

var str;
function a(){
  str="first";
}
function b(){
  var something = str +" second"; //new is reserved, use another variable name
}

Solution 2

Use function parameters, like this:

function a() {
   var str = "first";
   b(str);
}

function b(s) {
   var concat = s + " second";
   //do something with concat here...
}

You could just declare a variable higher up in the scope chain, but I opt to use arguments to restrict variable access to only the contexts that absolutely need it.

Oh yeah, isn't that called the principle of least privilege?

Share:
103,070

Related videos on Youtube

Hussein
Author by

Hussein

#Proficiency .in{ XHTML:CSS; } jQuery( '#Addict' ) <? echo "PHP Advanced"; ?> Mastery in Photoshop / Illustrator Contact me

Updated on July 09, 2022

Comments

  • Hussein
    Hussein almost 2 years

    Here are 2 functions in the simplest form. I'm working with jquery.

    What is the best way to pass var str from the first function to the second one?

    function a() {
        var str = "first";
    };
    
    function b() {
        var new = str + " second";
    };
    
  • Jacob Relkin
    Jacob Relkin over 13 years
    @Nick I like passing them around better than leaving them out in the open for others to modify.
  • Nick Craver
    Nick Craver over 13 years
    @Jacob - You're assuming a has anything to do with b though, it could just be a() in one place, possibly never even called, then b() in another.
  • Phrogz
    Phrogz over 13 years
    Man, it's really hard to decide which one of these answers to go with. Both are very, very valid given various uses, and there's no way to tell given the sparse details from the OP.
  • david
    david over 13 years
    The module pattern allows you to do this while still keeping the 'str' variable protected from outside editing. Quick example of a shared var: jsfiddle.net/Ma8Hs
  • Nick Craver
    Nick Craver over 13 years
    @david - true, but you'd have to proxy the functions outside, creating a closure so they can be accessed, complicated things a bit :)
  • David Tang
    David Tang over 13 years
    @Jacob, from this very limited example it's hard to see the OP's intended use of the variable. Choosing whether to pass as an argument, or declaring at a high scope, or even b.call(new String(str)) for arguments sake, really depends on the situation...
  • david
    david over 13 years
    @Nick, it's only complex if you say it out loud >.>
  • Hussein
    Hussein over 13 years
    What happens if we are referencing $(this) in var str. Will that still work in function b and retain the value of this that was referenced in function a.
  • Nick Craver
    Nick Craver over 13 years
    @alex - it depends on where the functions are called from, this will refer to the current context, but what that is depends on how they're called