Skip to content
jshacks.dev
Debugging

Inspect the last selected element with dollar zero

Use $0 in the DevTools console to refer to the element selected in the Elements panel, and $1 to $4 for the ones selected before it.

1 min read
Summarize in AI

Select an element in the Elements panel, then switch to the console. The variable $0 holds that element, so you can read it, change it or call methods without writing a selector.

JavaScript
$0.textContent
$0.classList.add("debug");
$0.getBoundingClientRect().width
Text
'Save'
undefined
84

The three results are for a button that says Save, which is 84 pixels wide. $1 is the element selected before $0, up to $4, which is handy for comparing two nodes: $0 === $1.nextElementSibling. These variables are listed in the Console Utilities reference.

You can also pass $0 to functions that take an element, such as getComputedStyle($0).color, or to monitorEvents($0, "click").

The values exist only in DevTools. They are not available in your own scripts, so do not use them in code. If you select another element, $0 changes, which can surprise you in the middle of a long expression.

Nothing is saved: $0 is undefined before you have selected anything in a fresh session.

Share

More snippets

Debugging

Group related logs with console.group

Indent related console output under a label with console.group and console.groupEnd, so interleaved logs from one request stay readable in Node.js 24.

1 min read
Debugging

Inspect an object with console.dir

Show nested objects beyond the default depth of two with console.dir and the depth option, instead of the [Object] placeholders console.log prints.

1 min read
Debugging

Debug a single Vitest test in VS Code

Run one Vitest file under the VS Code debugger with a launch configuration that passes the open file, so breakpoints in tests and source work. Vitest 3.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.