Home > Back-end >  Getting TypeError: hidden1 is not a function when storing function under let
Getting TypeError: hidden1 is not a function when storing function under let

Time:12-12

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();

CodePudding user response:

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.

  • Related