What I am doing wrong with this switch?

It’s been years since I last programmed in Java and had little to no experience with JavaScript back then so… I am trying to make a switch that when is activated the console will display “ON”, but instead, each time the switch changes, whether it is checked or not, the only message on the console is “OFF” and I am not sure what is it that I am doing wrong.
Please note, putting “;” at the end of “console.log()” is not affecting the result at all.

var S = $w('#Switch');
export function Switch_change(event) {
 if (S.checked === true){
    console.log("ON")
    } else {
        console.log("OFF")
    }
}

If you move the var assignment to the first line of the Switch_change instead of initiating above at the global level, it will work.

It works! Thank you very much.