How to create a global variable in Client side JS, where the variable should not be accessible from browser console ? I tried using let, it did not work (the code below is not in a function it is in a script file)
let data={
"data-item":"Secure Data"
};
I also tried adding data in a class and using an instance of it but the instance is also accessible from console. The variable is an object and will be modified dynamically by user interaction, so cannot freeze the object either.
If an identifier is global, it will be accessible from the console.
If you want something not to be accessible from the console, it will have to not be global. Easiest option would be to put everything into an IIFE. For example, turn this:
let data={
"data-item":"Secure Data"
};
// lots and lots of functions and variables that reference data
into this:
(() => {
let data={
"data-item":"Secure Data"
};
// lots and lots of functions and variables that reference data
})();
and then data
will be scoped only inside the IIFE, so it won't be accessible from the console, but everything that needs to access it will still be able to see it.