31

I am a beginner in JavaScript and wanted to ask the following: I have two simple functions and was wondering if there is any way to pass a variable value from one function to another. I know I can just move it outside the function to be used in other functions as well but just need to know the way how I can have one local variable and manipulate with it in my second function. Is this possible and how?

Here is some code:

window.onload = function show(){
    var x = 3;
}

function trig(){
    alert(x);
}
trig();

The question is: how do I access variable x (declared in the function show) from my second function trig?

Chris
  • 44,602
  • 16
  • 137
  • 156
user1393266
  • 431
  • 1
  • 5
  • 5
  • It's not very clear what you're asking... are you calling one function from within the other? Post some example code. – James Allardice May 14 '12 at 08:22
  • dont forget to mark answer as accepted if you got the info you want... – Pranay Rana May 14 '12 at 09:43
  • 1
    A straighforward way is to use the window object. Wherever you are inside your function use: `window.var1=42;` (var1 is your variable and 42 is your value. You can retrieve this variable from any other functions in your program. Easy ! – mchrgr2000 Apr 28 '22 at 14:17

3 Answers3

65

First way is

function function1()
{
  var variable1=12;
  function2(variable1);
}

function function2(val)
{
  var variableOfFunction1 = val;

// Then you will have to use this function for the variable1 so it doesn't really help much unless that's what you want to do. }

Second way is

var globalVariable;
function function1()
{
  globalVariable=12;
  function2();
}

function function2()
{
  var local = globalVariable;
}
Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • i used the second way and it worked. I dont come to this site just to "borrow" code. i like to understand exactly what occurs so i can see where the fallacy in my logic was. if anyone can explain further please do. In the second way, what i am seeing is a global variable being declared, a function that assigns that global variable a value of 12 and then a function being called within a function to concatenate the 2 functions together. then a function that is able to store the global variables value into a local variable. is that right?? – user2585548 Jan 29 '17 at 21:51
  • 1
    @user2585548 The first one does exactly what the question asks. It creates a local variable and passes its value to another function. The second function creates a global variable and sets a value in the first function. When the second function is called from the first function, a local variable is declared using the value of the global variable (set in the first function). – Puroto Jul 21 '17 at 12:50
21

Adding to @pranay-rana's list:

Third way is:

function passFromValue(){
    var x = 15;
    return x;  
}
function passToValue() {
    var y = passFromValue();
    console.log(y);//15
}
passToValue(); 
Asim Mahar
  • 1,320
  • 1
  • 10
  • 13
-1

You can very easily use this to re-use the value of the variable in another function.

// Use this in source
window.var1=  oEvent.getSource().getBindingContext();

// Get value of var1 in destination
var var2=  window.var1;
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Ankit Jain
  • 2,230
  • 1
  • 18
  • 25