DEV Community

Chinwendu Agbaetuo
Chinwendu Agbaetuo

Posted on

Creating functions in JavaScript - Part 2

Create a function with no parameters that prints how many times it’s been called.

let printCallTimes = 0;

const randomFunc = () => {
  printCallTimes++;
  console.log(printCallTimes)
  return printCallTimes;
}


randomFunc();
randomFunc();
randomFunc();

console.log(`The function has been called ${printCallTimes} times`);

Enter fullscreen mode Exit fullscreen mode
> 1
> 2
> 3
> "The function has been called 3 times"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)