What is the difference between window.innerWidth and screen.width?
It's kind of implicit in the names. :-) window.innerWidth
is the inner width of the window or more accurately viewport (not including toolbars, window chrome, etc.; but including the space occupied by the vertical scrollbar, if any). screen.width
is the width of the screen (not just the browser window).
So for instance, right now my browser window has an innerWidth
of 1197, but if I make it wider it could be (say) 1305. By the resolution of my screen is 1920x1080, so screen.width
on my machine will always be 1920, regardless of how big my browser window is.
But most importantly, I’ve heard one of them uses physical pixels, and the other one uses logical pixels.
They're both supposed to be in CSS pixels which I assume you'd call "logical" <insert pun here about CSS not being logical>, but note that there's no standard around this yet, just a working draft: screen.width
, innerWidth
. The draft says all measurements in it are in CSS pixels unless noted otherwise, and neither of those properties notes otherwise. If there are implementations out there using physical pixels for one and CSS pixels for another, I haven't heard of them (but I'm not sure I necessarily would have).
screen.width
is the width of the screen. For example, if your screen has resolution of 1600x900, screen.width
will always be 1600
.
window.innerWidth
though, is the inner width of the browser window. If you resize the browser window, window.innerWidth
will change.