JavaScript Tutorial

JavaScript Window

window is the browser tab: size, location, and timers. setTimeout and setInterval live here.

The tab object

window is the global object in a page. The current tab’s size, the URL, and the timer functions all live on it. In the browser you can write setTimeout without a prefix — that is still window.setTimeout.

document is window.document. You already used the document tree. This chapter is the tab around that tree.

Size

window.innerWidth and window.innerHeight are the viewport in pixels — the area your page can paint, not including browser chrome. They change when the visitor resizes the tab. Listen to resize if you need to react.

Example

<p id="out"></p>
<script>
  const out = document.getElementById("out");
  function showSize() {
    out.textContent = window.innerWidth + " × " + window.innerHeight;
  }
  showSize();
  window.addEventListener("resize", showSize);
</script>

Click Try it in JavaScript under an example. That opens/javascript/try — a live page and a console.

location

window.location is the current URL. location.href is the full string. location.pathname is the path. Assigning location.hrefnavigates. location.reload() reloads the tab.

Example

<p id="out"></p>
<script>
  const out = document.getElementById("out");
  out.textContent =
    "path: " + window.location.pathname +
    " — host: " + window.location.host;
  console.log(window.location.href);
</script>

In the StudyGrid editor the path belongs to the preview frame, not necessarily to the chapter URL. Log it to see what this tab thinks “here” is.

setTimeout

setTimeout(fn, ms) runs fn once after a delay in milliseconds.1000 is one second. It returns an id you can pass toclearTimeout if you need to cancel before it fires.

Example

<p id="out">Wait…</p>
<script>
  const out = document.getElementById("out");
  setTimeout(function () {
    out.textContent = "Done.";
    console.log("timeout fired");
  }, 1000);
</script>

setInterval

setInterval(fn, ms) runs fn again and again. Always keep the id and call clearInterval when you should stop — a clock, a poll, a short animation. Forgetting to clear leaves the function running after the visitor left that screen.

Example

<p id="out">0</p>
<button id="stop">Stop</button>
<script>
  const out = document.getElementById("out");
  const stop = document.getElementById("stop");
  let n = 0;
  const id = setInterval(function () {
    n += 1;
    out.textContent = String(n);
    if (n >= 5) {
      clearInterval(id);
    }
  }, 500);
  stop.addEventListener("click", function () {
    clearInterval(id);
  });
</script>

Other window names

NameJob
alert, confirmBlocking dialogs — fine for a test, rare in a finished UI
localStorageAlso window.localStorage — next chapter
fetchAlso on window — later chapter
scrollToMove the viewport

Global names you declare with var used to become properties ofwindow. let and const do not. Prefer not to park app data on window by hand.

What to remember

  • window is the tab: size, location, timers.
  • setTimeout runs once. setInterval repeats until you clear it.
  • innerWidth / innerHeight are the viewport.
  • location is the URL of this tab (or this frame).

Next: storage — strings that survive a reload, and strings that last only for the tab.

FAQ: JavaScript Window

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript window examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript window in this JavaScript JavaScript lesson (JavaScript Window).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.