How do I pass variables between functions in Javascript?

function, javascript, variables

Solution

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
}

Problem

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"; }; ```

Original source