JavaScript・Your syntax toolbox

Check things with the console

What's inside your variable right now? console.log is programming's dependable sidekick that lets you quietly check without putting anything on the page.

When you’re writing JavaScript, there are plenty of moments where you want to check “what’s inside this variable right now?” In those moments, console.log lets you quietly peek inside without putting anything on the page.

Let’s open the console

The console lives inside the developer tools that come built into your browser from the start.

  1. Right-click on the page → choose “Inspect” (or press F12 on Windows, Command+Option+I on Mac)
  2. From the tabs at the top of the panel that opens, choose “Console

That’s all the setup you need. This is where the “little notes” from JavaScript arrive. Open it up, and here’s what you see.

A browser screen. Shima’s page fills the left half, and the developer tools are open on the right half, with “Console” selected among the tabs at the top. Below it are four lines—“Hello!”, “Thanks for your message! It reached Shima.”, “age: 3”, and “city: Hokkaido”—with the script.js line numbers they came from shown at the right edge
On the left is the ordinary page, on the right the developer tools. The page’s appearance doesn’t change at all; only the contents of console.log line up on the right. The right edge of each line marks “which line of script.js it came from.”

Actually, the console isn’t just a place that waits to display things. You can type JavaScript straight into the input area and run it right there. Try typing 1 + 2 and pressing Enter—you’ll immediately get back 3. It’s also a little laboratory where you can quickly test “does this way of writing it work?” before you put it in a file.

Let’s write some

console.log("Hello!");

Write this in script.js and open the page, and “Hello!” shows up in the console. Nothing appears on the screen (the page itself). The place it appears is different—that’s the big difference from textContent.

PartMeaning
consoleThe console (where the notes are delivered)
.logThe request “please record this”
Inside ( )What you want to display

Checking what’s inside a variable

Where console.log really shines is when you want to check what’s inside a variable.

const thanksMessage = "Thanks for your message!";
console.log(thanksMessage);

“Thanks for your message!” shows up in the console. If you write the variable’s name without wrapping it in " ", you get the contents of the box.

Adding a label makes it easier to tell apart

As you have more things to check, a lone number sitting in the console can leave you thinking “wait, which variable was that?” In those cases, separate things with a , (comma) to pass the label and the contents together.

const age = 3;
const city = "Hokkaido";
console.log("age:", age);
console.log("city:", city);

The console shows them with names attached, like “age: 3” and “city: Hokkaido”. Since you can separate as many things as you like with commas, it becomes clear at a glance “which value is which.”

You can also check “how far did it run?”

When your code doesn’t work the way you expected, you can also use it to check “did it even run this far?”

console.log("Got this far!");

form.addEventListener("submit", function (event) {
  console.log("Button was pressed!");
});

If “Button was pressed!” doesn’t show up in the console, you know that pressing it isn’t triggering the inner code. It’s like a flashlight for narrowing down where the breakdown is.

TipsDon't panic when an error appearsFor how to read those red errors in the console, check out this handy tip

Summary of this lesson

  1. console.log(contents) lets you quietly display things in the console
  2. Open the console with right-click → “Inspect” → “Console” tab
  3. It’s a huge help for checking a variable’s contents, investigating “how far did it run?”, and spotting errors

Try it

The console.log you learned this time is not something you leave in Shima’s page. It’s a tool for “checking the contents” while you’re building. From here on, when your code doesn’t work the way you want, gently open the console and put it to use.

FAQ

Where does console.log show up?
It shows up in the "Console" tab of your browser's developer tools. You can open it with the F12 key on Windows, or Command+Option+I on Mac. It does not appear on the normal page that visitors see.
Is it okay to leave console.log in my code?
It's good manners not to leave it in a finished page. You write it temporarily to check something, then delete it once you're done. It won't break anything if it stays, but you should remove it so you don't accidentally reveal information you'd rather keep private.
How is console.log different from alert?
alert pops up a message on the screen of whoever is viewing the page and stops everything until they close it. console.log only shows up inside the developer tools and doesn't stop the page. For checking things as you build, console.log is the better fit.