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

Getting TypeError: hidden1 is not a function when storing function under let

My code works fine when I run it as a function but when I try to store it under let it says "TypeError: hidden1 is not a function" in the terminal. What am I doing wrong?

let counter = 0;

function hiddenCounter(){
  function addCounter()
  {
    counter++;
    return counter;
  }
  addCounter();
}

let hidden1 = hiddenCounter();
hidden1();

>Solution :

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

let hidden1 = hiddenCounter; // remove the parentheses

By removing the parentheses, you’ll assign the actual function to the hidden variable. In other words, with the parentheses, hidden1 is getting set to the output of executing hiddenCounter which is undefined because it returns nothing.

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