Global / Window

window.scrollTo

Scrolls to a particular set of coordinates in the document

Syntax

JavaScript
window.scrollTo(options) or window.scrollTo(x, y)

Parameters

ParameterTypeDescription
xnumber | ScrollToOptionsThe x-coordinate or options object
ynumberThe y-coordinate to scroll to

Return Value

undefined

Examples

Basic Usage
window.scrollTo({ top: 0, behavior: 'smooth' })
Practical Example
function scrollToTop() {
  window.scrollTo({
    top: 0,
    left: 0,
    behavior: 'smooth'
  })
}
Advanced Usage
const section = document.getElementById('features')!
const top = section.offsetTop
window.scrollTo({ top, behavior: 'smooth' })

Understanding window.scrollTo

The window.scrollTo method in JavaScript scrolls to a particular set of coordinates in the document. It belongs to the window object and is one of the most widely used methods for working with window values in modern JavaScript and TypeScript applications.

The method signature is window.scrollTo(options) or window.scrollTo(x, y). It accepts 2 parameters: x, y. When called, it returns undefined. Understanding when and how to use scrollTo() helps you write more expressive, readable code.

Common use cases for window.scrollTo include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-scrollintoview, dom-getboundingclientrect, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for window.scrollTo is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Global / Window Methods

Other methods in the Global / Window object

Related Tools

More Global / Window Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.