skip to content

In Chrome DevTools, when do you pin a Console live expression rather than save a Sources snippet, and what risk comes with each?

level: middleimportance: nice to knowfreq 20%

answer

  1. watching a value versus rerunning a script
  2. refreshes about four times a second
  3. preview guards, committed expression does not
  4. stored locally, not synced

basics

~20 s

Pin a live expression to watch a value that keeps changing, because DevTools re-evaluates it about every 250 ms. Save a snippet for a script you rerun on demand. Live expressions repeat side effects; snippets are local and unsynced.

solid answer

~50 s

I pin a live expression when I want to watch a value change, such as `document.activeElement` while tabbing through a form. I type it once with `Create live expression`, and DevTools re-evaluates it about every 250 ms while the Console pane is showing. The trap is side effects: DevTools only guards the preview while you type, so once committed the expression really runs each time, and `counter++` keeps climbing. An expensive expression gets repeated cost too. A snippet is for a script I want to run on demand, on any page, from the Snippets tab in Sources or from the Command Menu by typing `!` and its name. It runs in the open page's context, so it can change real data, and it is stored in DevTools' local preferences, neither synced nor on the file system. Anything a team needs belongs in the repo.

go deeper

for a junior

Recall the purpose of each: a live expression shows a value that keeps updating at the top of the Console, and a snippet is a saved script you run from Sources on any page.

for a middle

Explain the mechanics: refresh about every 250 milliseconds while the pane shows, evaluation in the selected context, and why a committed expression runs with real side effects while the typing preview does not.

for a senior

Choose reads for live expressions, so that you don't disturb what you are measuring, and remember that a snippet acts on the real page. Say which problems neither can solve, such as startup behaviour.

for a principal

The small team decision is where shared debugging helpers live. Snippets are local and unsynced, so anything worth keeping for others belongs in the repository, where it can be reviewed and found.

## Two tools for two rhythms Typing the same thing into the Console repeatedly is the signal that you want one of these two features. Which one depends on the rhythm of the work: - A **live expression** is an expression pinned to the top of the Console whose value keeps refreshing. Use it when the question is "what is this value right now, as I interact with the page?" - A **snippet** is a named script saved in the Sources panel that you run on demand. Use it when the question is "can I run this same chunk of code again, here or on another page?" Live expressions watch. Snippets act. ## Live expressions Click `Create live expression` in the Console toolbar, type an expression, then press Enter or click away. The result appears beneath it. You can pin several at once. The classic use is `document.activeElement`, which shows where keyboard focus is as you tab through a form, without having to retype it or log anything. Others are the length of a list while items load, or a flag in a state object while you click through a flow. Some details matter once you rely on them: - **Refresh rate.** The value updates about every 250 milliseconds. Each round waits for the previous evaluation to finish, so slow expressions refresh more slowly. - **Visibility.** Evaluation runs while the pane is showing and stops when it is hidden. - **Context.** The expression is evaluated in the Console's currently selected execution context, so changing the context selector changes which page or iframe it reads. - **Persistence.** Pinned expressions are saved in a local DevTools setting and come back after a reload. ## The side-effect trap While you are typing, DevTools evaluates the text with side effects refused and a short timeout, so the preview can't harm the page. That protection ends when you commit. A committed expression is evaluated for real on each round. This has consequences: 1. `counter++` increments the page's variable roughly four times a second for as long as it is pinned. 2. A function call with side effects, such as one that sends a request, runs again each round. 3. A costly query repeats, which can distort a measurement you are making in the same session. So pin reads, not actions. If you need to call something, make sure it is safe to call many times. ## Snippets Snippets live in **Sources** > **Snippets**. Choose `New snippet`, name it, write the script and save. To run it, click **Run**, press Ctrl+Enter or Cmd+Enter in the editor, or open the Command Menu and type `!` followed by the snippet's name. What defines a snippet: - **It runs on any page.** A snippet is not tied to one site, which makes it a good home for a helper such as dumping form state, or listing elements that have no accessible name. - **It runs in the page's context.** A snippet has access to the page's JavaScript, so it can read state and change it, which is the point and also the risk. - **It is local.** DevTools stores snippets as local preferences. They are not synced with your settings and can't be reached through the file system, so they stay with that DevTools profile unless you copy them out. - **It runs after the fact.** A snippet runs when you ask, on a page that has already loaded, so it can't change a startup that has already happened. ## Picking between them, and their neighbours | Need | Reach for | Why | |---|---|---| | Watch a value change as you interact | Live expression | Refreshes itself and takes no extra typing | | Rerun a multi-line helper on demand | Snippet | Named, saved and works on any page | | A single check, once | The Console prompt | Nothing to save | | Change how a script behaves at load | Local override | It alters the file the page receives | | Keep a helper for the whole team | The project's repository | Snippets stay in one person's local DevTools | ## How to answer in an interview State the difference in purpose first. Then give one concrete use of each, such as `document.activeElement` for a live expression and a form-state dump for a snippet. Finally, name a risk for each: repeated side effects on one side, and local-only storage plus real effects on the running page on the other. That last part is what many candidates leave out, and it shows you have actually used the features.

  • A pinned `refreshCart()` keeps sending requests. Why doesn't DevTools stop it?
    DevTools refuses side effects only for the preview while you are typing. Once you commit, the expression is evaluated normally on every round, so the function really runs about four times a second. Remove the pin, and pin a read of the state rather than a call that changes it.
  • Can a snippet fix how a page behaves during startup?
    No. A snippet runs when you ask, on a page that has already loaded, so startup code has already finished. To change what happens at load, use a local override, which replaces the file the page receives. A snippet suits inspecting or changing state afterwards.

  • A live expression can never change the page, since DevTools blocks all its side effects.
  • A live expression refreshes only when you press Enter or reload the page.
  • Snippets are stored as files you can commit from your overrides folder.
  • A snippet is sandboxed away from the page and cannot touch its variables.
  • Snippets sync across machines along with your other DevTools settings.