If statement - check boxes

I am trying to check and uncheck all with one js. What is wrong with my logic here? It is getting wierd responses, like it goes through checking them and then unchecks them.


////CHECK AND UNCHECK ALL////
function check()
{
var areThey = false;
var checkEM = document.getElementsByName("checkster");///checkbox name checkster
for (i = 0; i < checkEM.length; i++)

if (areThey == false) {
    
    checkEM*.checked=true;
    areThey = true;
    alert(checkEM.length + "check");
    
}

    else if (areThey == true) {
        
        checkEM*.checked=false;
        areThey = false;
        alert(checkEM.length + "uncheck");
}
}