What is the $ (Dollar Sign) Variable in Chrome DevTools? Uncovering the Native Function
If you’ve spent any time debugging JavaScript or inspecting web pages with Chrome DevTools, you’ve likely encountered the $ (dollar sign) variable in the console. At first glance, it might remind you of jQuery’s $ function, but Chrome DevTools’ $ is a native tool with a distinct purpose. In this blog, we’ll demystify the $ variable: what it is, how it works, its relationship to DOM selection, and how to use it effectively for debugging. Whether you’re a seasoned developer or just starting with DevTools, understanding this tool will streamline your workflow and make element inspection a breeze.
The $ variable in Chrome DevTools is not a JavaScript language feature nor a library (like jQuery). It’s part of DevTools’ Command Line API, a set of helper functions designed to simplify debugging directly in the console. Think of it as a shortcut for common tasks, specifically element selection.
By default, $ is an alias for document.querySelector, a native DOM method that selects the first element matching a CSS selector. This means $('selector') is equivalent to document.querySelector('selector')—but much faster to type!
At its core, DevTools’ $ is a wrapper for document.querySelector. This method takes a CSS selector string (e.g., '.nav-link', '#header', 'div') and returns the first DOM element that matches it. If no match is found, it returns null.
This behavior is intentional: $ exists to let you quickly grab elements without typing the full document.querySelector syntax. It’s a time-saver for debugging, allowing you to inspect, modify, or test elements in seconds.
While $ selects the first matching element, $$ (double dollar sign) is DevTools’ alias for document.querySelectorAll. It returns an array-like list (a NodeList) of all elements matching the selector—perfect for bulk operations.
// Change text color of all .nav-link elements to red $$('.nav-link').forEach(link => link.style.color = 'red');
Note: Unlike arrays, NodeLists don’t have all array methods (e.g., map, filter). Convert them to arrays with Array.from($$('selector')) for full functionality:
const links = Array.from($$('.nav-link')); links.filter(link => link.textContent.includes('Home')); // Now works!
$_ holds the result of the last evaluated expression in the console. For example:
2 + 2; // Output: 4 $_; // Output: 4 (returns the result of 2+2) $('.nav'); // Returns the .nav element $_; // Returns the same .nav element (last result)
No jQuery Methods: Unlike jQuery’s $, DevTools’ $ returns raw DOM elements. You can’t call .hide() or .on()—use native methods like addEventListener instead.
Overridden by jQuery: If the page loads jQuery, $ will refer to jQuery’s function. Use document.querySelector or $$ to bypass this.
Returns null for No Matches: Always check if an element exists before modifying it:
Chrome DevTools’ $ variable is a hidden gem for developers. As an alias for document.querySelector, it simplifies element selection, making debugging faster and more intuitive. Paired with $$ (for multiple elements) and other special variables like $0 and $_, it transforms the console into a DOM manipulation powerhouse.
Next time you’re debugging, skip typing document.querySelector—reach for $ instead. Experiment with selecting elements, modifying styles, and testing selectors, and watch your productivity soar!