As saving variables globally is not recommended, how could I save values in variables that are persistently maintained when loading the same page several times or when going to other pages and that the variable is not deleted?
For example, I store a private variable inside a module where I want to save or cache some values to have them later available.
const mod = (function() {
let privada = "foo";
return {
v: "hola",
f: () => {
console.log(mod.v);
console.log(privada);
v = "mundo";
privada = "bar";
}
};
}());
mod.f();
It's a pretty simple example but when reloading the page the variable has been re-initialized.
What would be the best way to be able to save variables in a private and persistent way, if possible? If it can't be done privately at least let me save it persistently?
Using Local Storage, it would be something like this:
NOTE: Doesn't work here... in this editor but you can try this example
Example with cookies:
See example here