How can I save a bunch of variables when someone leaves the tab?

I’m making a game, and I want to make it so that when you close the tab its saves. The game is like when you click a button, it updates a variable, but I don’t know how to save that variable. It is a numeric value, but I’m unsure whether or not localStorage works with that, but I’ve used it and it seems to just use strings.
I have this variable, but don’t know how to save it.

var clickvalue = 0 // the variable for the clicks

So there’s a button, and you click it, the value goes up. But when you close the tab, all the data is gone and you have to restart. Is there a way that I can save this?

I used localStorage, but it just thought it was a string.