Calling JavaScript sibling function inside parent function

I’m struggling with this and I haven’t found anything conclusive about this.

How can I call the function y() inside the function z() without altering the code's structure?

I tried using this but it didn’t work.

function x(){

    function y(){
    console.log("sal y")
  }
  
  function z(){
    console.log("sal z");
    y()
  }
 
}

Upvotes: 0

Views: 47

Answers (1)

Rory O'Kane
Rory O'Kane

Reputation: 30408

Your code inside z calls y in the correct way – that’s not the problem. The reason you’re not seeing output in your snippet is that you’re never calling function z. If I edit function x to call z, and I edit the top level to call function x, it works.

function x() {
  function y() {
    console.log("sal y");
  }

  function z() {
    console.log("sal z");
    y();
  }
  
  z(); // added
}

x(); // added

Upvotes: 2

Related Questions