which is a bit boring but expected. Yes, you need to keep track of the state somewhere outside of f, declaring an extra variable is the simplest solution. It doesn't need to be global though, set is hidden in the scope of the IIFE.