How to use a return value in another function in Javascript?

前端 未结 6 872
傲寒
傲寒 2020-12-03 02:02

I\'m self-teaching myself JavaScript and out of curiosity I\'m wondering what is the proper way of returning a value from one function to be used in another function. For ex

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 02:26

    Call the function and save the return value of that very call.

    function firstFunction() {
      // do something
      return "testing 123";
    }
    
    var test = firstFunction();  // this will grab you the return value from firstFunction();
    alert(test);
    

    You can make this call from another function too, as long as both functions have same scope.

    For example:

    function testCase() {
      var test = firstFunction(); 
      alert(test);
    }
    

    Demo

提交回复
热议问题