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.
- 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.infoprints asconsole.logdoes. So doesconsole.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 anon…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
undefinedis not shown, so a Snippet that ends withconsole.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 asifandfor. - Assignments,
i++anddelete. - 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.
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)andSet(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 asundefined. -0prints 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.
| (index) | name | year |
|---|---|---|
| 0 | JavaScript | 1995 |
| 1 | TypeScript | 2012 |
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.
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, untilconsole.groupEnd(). Click the label or its triangle to close it or open it.console.groupCollapsedstarts one closed.console.count("label")printslabel: 1, thenlabel: 2on the next call. With no label, it countsdefault.console.countResetstarts it at zero again.console.time("label")starts a timer.console.timeLog("label")prints the time so far, such aslabel: 1.234 ms, andconsole.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:
| Write | To print the next value as |
|---|---|
%s | Text. An object is printed as a value you can open. |
%d, %i | A whole number. Anything that is not a number prints NaN. |
%f | A number. |
%o, %O | A value you can open. |
%c | CSS 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.