MenuEveryday / The console

Docs / Everyday

The console

The console shows what a Run printed, its errors, and the value of its last line.

On this page

The console sits beside or under your code. It shows what each Run printed with console.log and the other console methods, in the order it was printed. It works as a browser’s console does, in Node Snippets and web pages alike.

What a Run printed

Each console.log call is one row. Values in one call are printed a space apart, so console.log("Hello", 42) prints Hello 42.

Each row ends with the line it came from. The value of the last line follows <, and a line printed later by a timer is marked late.
  • In a Node Snippet, output that arrives after your code’s last line has run, for example from setTimeout, wears a late badge.
  • The same row printed again straight after itself is shown once, with a count in front of it. Hold the pointer on the count to read Printed 3 times in a row.
  • console.info prints as console.log does. So does console.debug, in a quieter colour when Colored output is on.

Go to the line a row came from

The end of each row shows the line of your code it was printed from, such as L3.

  • Click it to put the cursor on that line in the editor.
  • Hold the pointer on a row, and its line is marked in the editor, when the editor shows that line.
  • In a web page, a row printed by an inline <script> or an on… handler in the HTML names a line of the HTML Part. Click it, and JSpark opens the HTML Part at that line.

The last value

When the last line of a Node Snippet is an expression, the console shows its value after a <. In the picture above, user.langs.length is the last line, so the console shows < 2. You don't need to wrap it in console.log.

  • The line runs once. JSpark keeps its value; it does not run it again.
  • A value of undefined is not shown, so a Snippet that ends with console.log(…) shows no extra row.
  • A last line with a //? Peek shows its value on the line instead.

To see the value of any other line, add a Peek.

Show every top-level value

Turn on Settings → Console → Show every top-level value to see the value of each top-level expression in a Node Snippet, not only the last one. Each value follows a <, in the order it was reached, with its line at the end of the row. It is off at first.

A top-level line is shown when it is an expression whose value you would ask for. These are left out:

  • Declarations, such as const total = 12, and other statements, such as if and for.
  • Assignments, i++ and delete.
  • Calls to console, which print themselves.
  • A line with a //? Peek, which shows its value on the line.
  • A value of undefined.

The setting is for Node Snippets. A web page’s console shows only what the page prints.

Objects

An object or an array is printed as one closed line, such as Object or Array(2). Click the chevron in front of it to open it, one level at a time.

The object opened: name is Ada, and langs is an array of two, still closed.

To print objects already open, click the last button in the footer. Its tooltip says Collapsed by default or Expanded by default. The same choice is Settings → Console → Expand objects, off at first.

The console prints values as they are, not as text:

  • Map(2) and Set(3) open to their entries. A Map entry shows its key, then =>, then its value.
  • An empty slot in an array shows as <empty>, not as undefined.
  • -0 prints as -0.
  • A getter shows as (getter). The console does not call it.
  • An object that contains itself shows (already shown above) where it comes back.
  • A very large object is cut short, with a line such as … 250 more entries.
  • console.dir(value) prints its first argument as a value you can open. In a web page, it shows an element’s properties rather than its tag.

Tables

console.table(data) prints the value, and a table under it:

  • One row for each item of an array, each property of an object, or each entry of a Map or a Set. The first column, (index), names it.
  • One column for each property of the rows, in the order they first appear.
  • A Value column for rows that are not objects, such as numbers or strings.
console.table prints the array, then a table: (index), then a column for name and one for year.

To show only some columns, name them: console.table(langs, ["name"]). A table shows its first 20 columns and its first 1,000 rows, then a line such as … 4000 more rows. A wide table scrolls sideways. A value that can’t be a table, such as a number, prints as an ordinary row.

Warnings and errors

console.warn prints a row tinted in the warning colour, with a warning icon. console.error prints a red row with an error icon. The picture above shows one of each. Click the triangle at the start of the row to see the functions it was called from.

An error your code throws is a red row too: its message, then the functions it came through, one per line. Each line in your own code names the function and its line, such as total — line 2. Your Snippet’s own body is top level.

total() was called with nothing, so items is undefined. The error came from line 2, called from line 6.

Click one of those lines to put the cursor on that line in the editor. An error that names a line, such as a syntax error, is a link too. In a web page, a frame in an inline script or handler reads HTML line 4 and opens the HTML Part.

What printed before the error stays in the console: in the picture, 12 from the first call. The footer says Threw and how long the Run took.

console.assert(condition, message) prints an error row that starts with Assertion failed:, only when the condition is false. console.trace() prints a row with its functions already open.

Groups, counters and timers

  • console.group("label") starts a group: the rows after it are indented under the label, in bold, until console.groupEnd(). Click the label or its triangle to close it or open it. console.groupCollapsed starts one closed.
  • console.count("label") prints label: 1, then label: 2 on the next call. With no label, it counts default. console.countReset starts it at zero again.
  • console.time("label") starts a timer. console.timeLog("label") prints the time so far, such as label: 1.234 ms, and console.timeEnd("label") prints it and ends the timer.
  • A timer or a count that doesn’t exist prints a warning, such as Timer 'label' does not exist.

Format strings

When the first value is a string, it can hold these, as in a browser:

WriteTo print the next value as
%sText. An object is printed as a value you can open.
%d, %iA whole number. Anything that is not a number prints NaN.
%fA number.
%o, %OA value you can open.
%cCSS for the text after it.
%%A % sign.

So console.log("%s is %d", "Ada", 36) prints Ada is 36. With %c, the CSS can set colour, background, font, text decoration, padding, margin and border. A style stays inside its own row, and nothing in it is loaded from the network: a url() is ignored.

Clear the console

Do one of these:

  • Press ⌘K or Ctrl+K.
  • Choose Run → Clear Console.
  • Click the Clear console button in the footer.

Your code can call console.clear() too. The rows before it go, and the console says Console was cleared.

In the Timeline, the console can’t be cleared: its lines belong to the recording. The button’s tooltip says Leave the Timeline first.

Good to know

  • Settings → Console → Colored output colours values by type: strings, numbers, keys. It is off at first. Warnings and errors are tinted either way.
  • Settings → Console → Text size follows the editor’s size until you set the console apart.
  • Peek values are drawn beside the code, never in the console.

The pictures show JSpark on a Mac. On Windows, the window buttons are at the top right, the menus are in theMenu button at the left end of the tabs, and shortcuts use Ctrl where a Mac uses ⌘. The rest works the same way.

Esc

Type to search every docs page.

JSpark is downloading

When you open JSpark-Setup.exe, Windows may stop it once, because the installer isn’t code-signed yet. If the file came from jspark.app:

A blue Windows box, “Windows protected your PC”: Microsoft Defender SmartScreen prevented an unrecognised app from starting. Under it, the link More info, and the button Don’t run.

1Click More info.

The same box after More info: Application JSpark-Setup-1.4.0.exe, Publisher Unknown publisher, and the buttons Run anyway and Don’t run.

2Then click Run anyway.

You see this only once: updates install inside the app. More in Install JSpark.