CHANNELS:

All
S
window.innerWidth vs innerWidth
Canvas
Swebd posted 5 years ago

Hi Chris

First of all my compliments for the construction of the videos and website!

I have a question about innerWidth.

At the top of the code, use

canvas.width = window.innerWidth;

but for example to empty the canvas you only use innerWidth without window.

ctx.clearRect (0,0, innerWidth, innerHeight);

Did I miss a declaration at the top of the innerWidth code? For me it also works without declaration from innerWidth.

So my question is why you sometimes use window.innerWidth and sometimes only innerWidth?

Thank you in advance for your answer!

chris posted 5 years ago

Hey Swebd, great question!

So, the reason I use innerWidth sometimes instead of window.innerWidth is that by default, you can access any window property within a JS file as long as it's being read by the browser.

You technically don't need window at all, as long as you know the property name associated with the window object, you can take out the window portion altogether.

The main reason you'd want to keep that window. prefix is for clarity purposes. In my case, I already know innerWidth and innerHeight are window properties, so I don't really include it in most of my projects unless I absolutely have to.

0
S
Swebd posted 5 years ago

I understand! Thanks for the quick explanation and keep it up!

1
d
dbaxi posted 5 years ago

0

Want to participate?

Create a free Chris Courses account to begin

or
Sign In

Providing the lift to launch your development career

© 2025 Chris Courses. All rights reserved.