JavaScript Window scrollTo() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.scrollTo() scrolls to a particular set of coordinates in the document (MDN). Pass (x, y) or an options object with top, left, and behavior. For a relative nudge from the current spot, use scrollBy().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.scrollTo. Start with this line.

JavaScript
scrollTo(x, y) or scrollTo(options)
CallWhat it does
scrollTo(x, y) or scrollTo(options)Primary signature.
window.scrollTo(...)Call with the arguments your case needs.

Built into the browser. Call window.scrollTo(...) (or the global scrollTo(...)) — no import required.

Library declarations
JavaScript
scrollTo(x, y) or scrollTo(options)

Parameters

ParameterDescription
options.behaviorOptional. smooth, instant, or auto

Return Value

Return ValueDescription
*Promise<{ interrupted }> (support varies)

JavaScript Examples

Example 1 — Jump to Coordinates

MDN basic usage: scroll to an absolute document position.

JavaScript
window.scrollTo(0, 500);
console.log("scrollY:", window.scrollY);
Try It Yourself

How It Works

  1. Absolute. Sets vertical scroll to about 500.
  2. x = 0. Left edge stays at the document origin.
  3. Idempotent. Call again — still ~500, not 1000.
  4. Clamp. Past the max, the browser stops at the end.

Example 2 — Smooth Back to Top

MDN demo pattern: scrollTo(0, 0) with smooth behavior.

JavaScript
window.scrollTo({
  top: 0,
  left: 0,
  behavior: "smooth",
});
Try It Yourself

How It Works

  1. top: 0. Document top is the target.
  2. smooth. Animates instead of jumping.
  3. UX classic. Common “Back to top” button pattern.
  4. Await optional. Use the Promise when you need “done.”

Example 3 — Options with top and left

MDN options form for both axes.

JavaScript
window.scrollTo({
  top: 100,
  left: 100,
  behavior: "smooth",
});
Try It Yourself

How It Works

  1. Object form. Clearer when setting behavior.
  2. Both axes. top and left are absolute.
  3. Wide pages. Horizontal scroll needs overflow content.
  4. Same idea. Matches the two-argument form.

Example 4 — Await the Promise

MDN: respond when a smooth absolute scroll finishes; check interrupted.

JavaScript
async function goTop() {
  const result = await window.scrollTo({
    top: 0,
    behavior: "smooth",
  });
  console.log("interrupted?", result.interrupted);
}

goTop();
Try It Yourself

How It Works

  1. Await finish. Useful after smooth “Back to top.”
  2. interrupted. True if another programmatic scroll started first.
  3. Feature-detect. Test window.scrollTo(0, 0) instanceof Promise.
  4. Fallback. If no Promise, the scroll still runs — just no await.

Example 5 — Log scrollY After Jump

Confirm the absolute target after scrollTo.

JavaScript
const before = window.scrollY;
window.scrollTo(0, 300);
const after = window.scrollY;

console.log("Before:", before);
console.log("After:", after);
Try It Yourself

How It Works

  1. Snapshot. Store scrollY before the call.
  2. Jump. Absolute target y = 300.
  3. Compare. After should be ~300 (unless clamped).
  4. Smooth note. With behavior: "smooth", read again after animation if needed.

Browser Support

Window.scrollTo() is marked Baseline Widely available on MDN (since July 2015). It is not Deprecated, Experimental, or Non-standard. Promise return / interrupted may vary — feature-detect if you depend on it. Browser Window API — not a Node.js built-in.

✓ Baseline · Widely available

scrollTo()

Scroll the document to absolute coordinates across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Safe for modern apps. Prefer options + behavior for smooth UX; detect Promise support before awaiting.

Frequently Asked Questions

It scrolls the document to a particular set of coordinates (MDN). scrollTo(0, 500) moves so the viewport’s top-left is at document position (0, 500), unless clamped at the edges.
No. MDN marks Window.scrollTo() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Some parts (such as the Promise return) may vary by browser.
MDN documents a Promise that fulfills with { interrupted }. interrupted is true if another programmatic scroll started on the window before the first finished. Feature-detect if you rely on the Promise.
scrollTo(x, y) sets an absolute position. scrollBy(dx, dy) adds a relative delta from where you are now. Use scrollTo for “go to top” or a known coordinate; use scrollBy for “move one screen from here.”
Optional string: smooth (animate), instant (jump), or auto (use the computed CSS scroll-behavior). Default is auto (MDN).
scrollIntoView() is usually simpler for a target element. Prefer scrollTo when you need exact document pixel coordinates (for example back-to-top at 0, 0).

Next: setInterval()

Run a function repeatedly on a timer.

setInterval() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful