Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

function createCounter(initialValue) { let counterValue = initialValue;

function increment() { counterValue++; console.log('Incremented:', counterValue); }

function decrement() { counterValue--; console.log('Decremented:', counterValue); }

function getValue() { console.log('Current value:', counterValue); }

return { increment, decrement, getValue }; }

// Create a counter with an initial value of 5 const counter = createCounter(5);

// Use the counter counter.increment(); // Output: Incremented: 6 counter.increment(); // Output: Incremented: 7 counter.decrement(); // Output: Decremented: 6 counter.getValue(); // Output: Current value: 6

About

The code defines a createCounter function that takes an initialValue parameter. This function creates a closure that encapsulates the counterValue variable and the inner functions increment, decrement, and getValue.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors