So far I am trying with:
var myBoolean = false; // global
function toggleBoolean(vr) {
vr = !vr;
}
alert(myBoolean); // false
toggleBoolean(myBoolean);
alert(myBoolean); // false
But obviously, It failed.
Edit: sorry, I forgot to point out that I want the function to work with many Booleans and not just one
Yes, you can toggle a global boolean from a function. You can’t do it as in your attempt because JavaScript is strictly call-by-value.
Now, if you want to toggle a global by name, you could do this (though it’s a little icky):
Global variables (in JavaScript on a browser) are properties of the global object, which is known as “window”. In other contexts there are ways of associating a name with the global context. You’d call that function, therefore, like this:
Note that I pass a string there instead of a reference to the actual global variable.