Edit in JSFiddle

var logDiv = document.querySelector('#log');
var small = window.matchMedia('(max-width: 640px)');

var smallViewport = (e) => {
  if (e.matches) {
    /* viewport er 640 pixels eller mindre */
    logDiv.textContent = 'Small — smallere end 640px.';
  } else {
    /* viewport er bredere end 640 pixels */
    logDiv.textContent = 'Medium-up — bredere end 640px wide.';
  }
}

small.addListener(smallViewport);
smallViewport(small);
<div id="log"></div>