Joel Thoms
Aug 8, 2017 · 1 min read

You are correct. There are some tricks to storing state. That is what a lot of FP is about, How to manage mutations and state. Everything cannot be pure. There are some fun things you can do with this too. For example, check out this Counter block:

const Counter = (state = 0) => () => state++

The state is encapsulated in a closure, and then returned as a function. Every time that function is called, it mutates the encapsulated state.

You would create it like this:

const counter = Counter(0)counter() // => 0
counter() // => 1
counter() // => 2
counter() // => 3

We have just created a Counter that holds state and without any globals!

It is similar to a poorman’s redux.

    Joel Thoms

    Written by

    Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade