Creating functions in JavaScript - Part 2

dindustack

Chinwendu Agbaetuo

Posted on April 12, 2024

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
💖 💪 🙅 🚩
dindustack
Chinwendu Agbaetuo

Posted on April 12, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related