How many <li> are on this page? Which classes does that button have? Small questions like these don't need a new file. Type them into the Console and the page answers right away.
The Console panel in DevTools has two jobs: showing messages from the page (logs, warnings, errors), and running JavaScript you type, right inside that page. The Console is like a walkie-talkie to someone inside a house, and the house is the page. You can ask "how many lights are on?" (code that reads the page) or say "turn off the kitchen light" (code that changes the page), and the answer comes back right away. But whatever you change through the Console only lasts until the page reloads. Your code files don't change.
Opening the Console
Ctrl+Shift+J(Mac:Cmd+Option+J) opens DevTools directly on the Console tab.- If you're in another panel, such as Elements, press
Esc. The Console appears as a drawer under that panel, so you can pick elements while you type.
Type JavaScript, the page answers
The Console is a REPL (Read, Eval, Print, Loop). This means the Console reads what you type, runs it, prints the result, then waits for the next line. The code runs inside the page that's open. So that code can use document and that page's global variables. It's the best place to try code without changing a file.
The example page, a to-do list with three items. This page's CSS (not shown here) crosses out every item that has the class done:
<title>Tasks for today</title>
<h1>Tasks for today</h1>
<ul>
<li class="done">Read the error message</li>
<li>Add a console.log</li>
<li>Write the fix</li>
</ul>
This is what happens when you type three commands into that page's Console:
> document.title
<· 'Tasks for today'
> document.querySelectorAll("li").length
<· 3
> const total = 3
<· undefined
>is what you type,<·is the Console's answer.- Text is shown in quotes, numbers without.
const total = 3is a statement, not a value, so the answer isundefined. That's normal, not an error. The variabletotalis still stored.- While you type, the Console already shows a grey preview of the result (eager evaluation).
Shift+Entermakes a new line without running the code, and the up arrow brings back the previous command.
Special Console variables
| Type | It means |
|---|---|
$0 |
the element selected in the Elements panel right now ($1 to $4 the earlier picks) |
$_ |
the result of the last command |
$("li") |
the same as document.querySelector("li") |
$$("li") |
every match, as an array (like document.querySelectorAll) |
copy(value) |
copies the value to the clipboard, so you can paste it somewhere else |
> $$("li").length
<· 3
> $_ * 10
<· 30
> $0
<· <li>Write the fix</li>
> $0.classList.add("done")
<· undefined
$_ holds the last result, 3, so $_ * 10 gives 30. Why it helps: you don't have to retype a long result, or store it in a variable. In this example, the third item was already selected in the Elements panel, so $0 is that item. After classList.add("done"), that item is crossed out on the page right away. The answer is undefined because add doesn't return anything.
These variables only exist in the Console. In a .js file, $$("li") causes ReferenceError: $$ is not defined. And if the page uses jQuery (an older JavaScript library that uses the name $), then $ in the Console belongs to jQuery and is no longer short for document.querySelector.
Filtering messages
- The Default levels menu picks which message levels show: Verbose, Info, Warnings, Errors.
- The Filter box hides messages that don't contain the text you type.
Ctrl+Lclears the Console.- In the Console settings (the gear icon), Preserve log keeps messages from disappearing when the page navigates or reloads. A page navigates, for example, after a form is sent.
- Errors show in red, with the file name and line number on the right. Click that file name, and the Sources panel opens right at that line.
💡 Live expression
The eye icon above the Console (Create live expression) pins an expression (code that produces a value) that's calculated again all the time. For example, document.querySelectorAll("li.done").length goes from 1 to 2 the moment you cross out an item, without retyping anything.
⚠️ Console habits that get you into trouble
- Using
$or$$in a code file. They're Console helpers. In code, usedocument.querySelectoranddocument.querySelectorAll. - Thinking changes from the Console are saved. Everything you change is gone on refresh. Move what works into your file.
- Messages vanishing after a form submits. The page navigates, and the Console is cleared. Turn on Preserve log.
- Pasting code from strangers. Code in the Console has access to the page, including the account that's logged in. If you have used the Console only a little (fewer than 5 commands in its history), Chrome blocks the paste and asks you to type
allow pastingfirst. Once you have typed it, Chrome doesn't ask again. Don't paste code you don't understand.
TL;DR: The Console panel in DevTools (Ctrl+Shift+J, or Esc from another panel) shows messages and runs JavaScript inside the page. So it's the best place to try code without changing a file. > is what you type, <· is the answer, and a statement such as const x = 3 answers undefined. Special variables: $0 the element selected in the Elements panel, $_ the last result, $() and $$() short for querySelector and querySelectorAll, copy() copies a value to the clipboard. They only exist in the Console. Filter messages with the Default levels menu and the Filter box, turn on Preserve log so messages stay when the page navigates, and remember: changes from the Console are gone on refresh.
⚡ Type JavaScript, the page answers
Write:
> document.querySelectorAll("li").length
<· 3
> const total = 3
<· undefined
Guess first: > is what you type in the Console, <· is the answer. const total = 3 answers undefined. Is that an error?
No. It's a statement, not a value, so the answer is undefined. The variable total is still stored.
Meaning: The Console panel in DevTools (Ctrl+Shift+J) runs JavaScript inside the open page. So it's the best place to try code without changing a file.
⚡ Special Console variables
Write:
> $$("li").length
<· 3
> $_ * 10
<· 30
Meaning: $$("li") is like document.querySelectorAll("li"), but the result is an array. $_ holds the result of the last command, here 3. $() is short for querySelector. They only exist in the Console. In a .js file, $$("li") causes ReferenceError: $$ is not defined.
⚡ Changes from the Console are gone on refresh
Meaning: $0 is the element selected in the Elements panel. $0.classList.add("done") adds the class done to that element. If the page's CSS crosses out elements with the class done, that element is crossed out right away. But your code file doesn't change. Refresh, and the line through it is gone, so move what works into your file. Messages vanishing when the page navigates? Turn on Preserve log in the Console settings to keep them.