DEV Community

Chinwendu Agbaetuo
Chinwendu Agbaetuo

Posted on

1 1 1 1 1

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs