Passing a local variable from one function to another

后端 未结 3 659
独厮守ぢ
独厮守ぢ 2021-01-30 09:29

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 an

3条回答
  •  天命终不由人
    2021-01-30 10:09

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

提交回复
热议问题