Dev Tools Tricks: Store objects and elements as variables in the console
Tags: devtools, chrome, firefox
Published: 2017-01-12 14:56:38

---
title: "Dev Tools Tricks: Store objects and elements as variables in the console"
published: true
description: 
cover_image: http://philna.sh/images/dev_tools_tricks_header.png
tags: dev tools, chrome, firefox
canonical_url: https://philna.sh/blog/2017/01/12/dev-tools-tricks-store-objects-and-elements-as-variables-in-the-console/
---

Browser dev tools are so full of features it's hard to keep up. I bet every developer knows a different set of features to each other. I wanted to share a few little tips that I use, that you might not know and that if I don't write down I might forget.

Read on and discover the magic of `$0`, `$_` and `temp0`.

## Access selected elements as variables

When you're clicking around the Inspector in Firefox or the Elements tab in Chrome and you want to use the selected element in the console, you need a reference to that element. You could type out a `querySelector` that would pick the element out of the document, but that's far too much work. Instead, leave the element selected and use `$0` in the console to get the reference you need.

![Typing $0 in the Firefox dev tools console will grab a reference to the currently selected element.](https://philna.sh/assets/posts/dev-tools-tricks/firefox_dollar_zero-510b6a988373a6f07bcdaa3f3721bdbfa3dbd7d8eddd5627c6c9a0511abd0407.png)

Chrome even hints at this by showing `== $0` next to the HTML.

![Typing $0 in the Chrome dev tools console will grab a reference to the currently selected element.](https://philna.sh/assets/posts/dev-tools-tricks/chrome_dollar_zero-a369679f67b420864f9d2e8d257ee77483982c0d99b3def3019881de16d6f859.png)

Be aware though, if you select a different element in the inspector then it will become the subject of `$0` and you will lose the reference to the old one.

Firefox has another trick for this. Right click on an element and choose "Use in console". This places a variable called `temp0` into the console's command line which is a reference to that selected element.

![In Firefox you can right click on an element and choose 'Use in console' to use it in the console as a variable named temp0.](https://philna.sh/assets/posts/dev-tools-tricks/firefox_use_in_console-671b0e3849c059eea10ce109891e16c14aedf67f955ffb6451af0dec2ef9c015.png)

## Turn any object in the console into a global variable

If you're dealing with other objects in the console, perhaps something you've logged from your own code, and you want to reference them then there are a couple of ways to do that too.

`$_`, for example, is a reference to the last object that was returned in the console.

![Typing $_ in the dev tools console will grab a reference to the last returned object in the console.](https://philna.sh/assets/posts/dev-tools-tricks/firefox_dollar_underscore-cae5a1e421b153f749cf02db5b332ac06c043942072a5f72f30ea1df3b7549ff.png)

More generally, any object that has been returned or logged to the console can be turned into a global variable by right clicking on it and selecting "Store as global variable". You will get a variable called `temp0` which references that object. Even better, do it for more objects and you'll get new variables called `temp1`, then `temp2` and so on.

![Right click on any object in the console and choose 'Store as global object' to save it as a variable.](https://philna.sh/assets/posts/dev-tools-tricks/store_as_global-bb26e1820c7c4c855487ad00ce61eb3857e7c1ad16eabb882eb7dcedd0e75204.png)

## Dev tools go deep

There is so much to learn about dev tools, these are just a few little tricks that might help when debugging your code. If you're looking for more tips like this, I recommend signing up to [Umar Hansa's Dev Tips](https://umaar.com/dev-tips/) or watch his [ffconf 2016 talk](https://www.youtube.com/watch?v=N33lYfsAsoU&list=PLXmT1r4krsTpDoGcdh1baZPIV6DtX9_rX).

Don't forget to share your own tips or things you've found in dev tool. If you've got any good ones, please send them to me on Twitter at [@philnash](https://twitter.com/philnash).

___

_[Dev Tools Tricks: Store objects and elements as variables in the console](https://philna.sh/blog/2017/01/12/dev-tools-tricks-store-objects-and-elements-as-variables-in-the-console/) was originally published at [philna.sh](https://philna.sh) on January 12, 2017._