DEV Community

Cover image for Two gotchas in Chrome Developer Tool Console
Sung M. Kim
Sung M. Kim

Posted on • Originally published at slightedgecoder.com on

4

Two gotchas in Chrome Developer Tool Console

Photo by Jason Leung on Unsplash

There are two surprising behaviors on Chrome Developer Tool Console (“console” hereafter).

First one was pointed out by my friend Nicolas Marcora that you can await an async method, and second one being $$ (a short-cut for document.querySelectorAll) returning an array, not a NodeList object.

Let’s go over how they are different.

1️⃣ Await in console

Within an editor (I am using a Snippets feature, which is like a scratchpad but works like an editor), await does not work as it needs to be called within an async method.

error in snippet

To get around the issue, you can wrap it in an async method (an async IIFE in this case).

wrapped in async iife

This would be the normal behavior you are expecting but…

You can await in the console without wrapping the statement in an async method~

works in console
It’s Magic~~~

It’s a behavior added to Chrome Devtools since Chrome 62, released on year 2017.

So this is a nice feature but you have to watch out as you can’t simply paste your code in your editor.

It needs to be wrapped inside an async method.

2️⃣ $$ vs document.querySelectorAll

$$ is a part of Console Utilities API, which is available only within the console and not part of either JavaScript or DOM.

Google document describes $$ as

$$(selector) returns an array of elements that match the given CSS selector. This command is equivalent to calling document.querySelectorAll().

https://developers.google.com/web/tools/chrome-devtools/console/utilities#queryselectorall

The documentation says it’s equivalent to calling document.querySelectorAll() but $$ is differs

where document.querySelectorAll() returns a NodeList object while $$ returns an array.

nodelist result in cnosole

NodeList is an array-like object, whose prototype doesn’t inherit from Array.prototype. That means, a NodeList object instance doesn’t have access to methods such as Array#map or Array#reduce.

Can’t map over NodeList object
Can’t map over NodeList object

While you can map over $$
While you can map over $$

This can cause a problem when you copy & paste code using $$ selector and simply convert it to using document.querySelectorAll() and try to call Array.prototype

You can easily convert a NodeList object to an array using a spread syntax or Array.from by the way.

workaround using spread and array from

👋 Parting Words

The console can save you a lot of keystrokes but you might want to double check before copying & pasting the code from console to the editor.

If you have more gotchas please let me know 🙂

The post Two gotchas in Chrome Developer Tool Console appeared first on Sung's Technical Blog.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay