When the browser loads your HTML it builds a tree of objects. JavaScript can read and change that tree, and the page updates instantly.
document.querySelector('.box')It takes a CSS selector (the exact same syntax you already write in your stylesheet) and returns the FIRST match. querySelectorAll returns all of them.
querySelector('.box') with the dot finds a class. querySelector('box') without it looks for a <box> TAG, finds nothing, and returns null, then the next line fails with "cannot read properties of null". If you see that error, check your selector first.