Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function x(){

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading