I think this is because you have only min-width defined. if you are searching for min-width:320px - this query is met always - even you have bigger resolution. Try something like
if (window.matchMedia("(min-width: 320px) and (max-width: 767)").matches) {
alert("width 320"); // works
}//one px less then the query in next statement