Can content scripts have status variables?

I’d like to initialize a few variables after the document (respectively the content script) is loaded, and use them multiple times later on in event handlers.

The status variables should persist as long as the document exists. They should get lost when the tab is closed or another document is loaded, and should be reinitialized when the tab is reloaded.

How can I do that ?

I’ve tried to just add some variables to the script, but when I try to use them in event handlers, I get errors about uninitialized variables.

This is not related to content scripts but JavaScript in general.
Make sure to study the MDN, it will explain you in what scope you can use them: