0

I am looking for someone to explain me something since I can't find a clear answer. My question is about functions that call themselves. I saw that it is possible to build a list of functions that are 'chained' together and for example the first function calls the second one then the second one calls another one. My confusion is : Lets say that you have a second function that has a variable let a = 12; if i call that function on the first function, will i have access to that variable or whatever that second function might have inside? How can i pass info to another function? can a function can be dependent on another function in order to complete a task? Thanks in advance guys.

Edit to make it more clear what I mean:

function first(){

    second(); 

    // will i have access to whatever there is inside function second since i am calling it here ? or it doesn't work that way?
}

function second(){
    let a = 12;

    third();
}

function third(){
    fourth()....
}
deceze
  • 510,633
  • 85
  • 743
  • 889
Why u do dis
  • 418
  • 4
  • 15
  • 1
    I think it would be helpful for us if you attempted to write out what you're thinking in code or pseudo-code. Currently your question is a bit ambiguous. Which means that we wont be able to help you efficiently, or at all. – Olian04 Aug 28 '20 at 06:25
  • 1
    Read https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript , on that post you can also see how values are passed to functions. – Teemu Aug 28 '20 at 06:27
  • Reading between the lines, you seem to be asking whether Javascript has [dynamic scoping](https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scoping). It does not. – deceze Aug 28 '20 at 06:30
  • No, `a` is only in scope *within `second`* or any function *declared* within `second`. Functions declared outside `second` do not share any scope with `second` and do not have access to `a`. – deceze Aug 28 '20 at 06:32

0 Answers0