I think this might be my most popular tweet of all time, but I’m not sure how to verify that these days. I’ll restate this neat little trick here because blogging is cool and fun.
I used to do this a lot while debugging JavaScript:
console.log("myVariable: ", myVariable);
But now I do this because it’s just easier to type quickly:
console.log({ myVariable });
And you don’t miss out on anything in DevTools:

Now that this is a blog post, I can elaborate a smidge…
The output display there (and really, console.log
itself) is a DevTools thing, but the syntax isn’t. By using curly brackets in JavaScript, I’m creating an object. I don’t have to assign an object to anything, it can just be.
{
foo: "bar"
}
You see that a lot when passing an object to a function, like myFunction({ config: options });
. The “trick” is that when you’re making an object, you can “shorthand” it by providing only a variable, which makes it implied). So like:
const fruit = "apple";
// Shorthand
let x = {
fruit
}
console.log(x);
// Normal way, literally the same exact thing
x = {
"fruit": fruit
}
console.log(x); // identical
When you have a variable and you log it like console.log({ myVariable });
you’re using that shorthand object creation syntax and it gets logged like the object it becomes.
One strike against this idea is that sometimes DevTools chooses to output it as ▶ Object and you have to click to open to see the value. I don’t know what the heuristics are of when it chooses to do that and when it doesn’t. If you don’t like that, or the output format of an object in general, you might prefer the console.table({ myVariable });
format:

Clever tip, but even faster is to add snippets in your text editor for common things like this. In Sublime Text I map
l;
toconsole.info('${1}', ${2:$1});
. Then I can just typel;<tab>someVarName
and it expands with the var name filled out in both places.Same works in VS Code, but you need to tweak some settings to get it to expand instantly on .
excellent!
This usually happens when the Dev tools are closed when the value is printed.
Appreciation! Thank for the enlightenment!
There is a gotcha to this. When you expand the ▶ Object, you are seeing the current value, not the value as it was when
console.log
ran. Take this example:When you open the console and expand ▶ Object, you will see
{example: {foo: "second"}}
, not{example: {foo: "one"}}
. I know this can happen whether or not you use the trick explained in the blog post, but I thought it was worth pointing out.My first Js debugging was alert( myvariable ); and it was pretty annoying.
Hi Chris,
Thanks for such as awesome trick.
I am not big fan of using Curls braces in JS however, the console.table() function caught my attention.
It lool cool on console. Also I’m thinking to improve/implemet this trick on one of my latest blog post on JS https://letstacle.com/javascript-hello-world-example-step-by-step-explained-code
You feed back will be valuable to me. Feel free to check it out.
Keep up the great work.
This is cool trick but pay attention – the object you just created is alive and in some situation may not go to the garbaege collector
Thank you Chris! This is a nice one!
I additionally still often use
debugger;
in my code – or is this not a thing anymore?! So I can click around through the stopped code state and find out interesting things.That’s an awesome idea!!!
When I fist saw the title of this post, I have already known what is it going to say, but… wondered how didn’t this trick come to my mind earlier, as I know the concept behind it and was looking for a way to do this!
Thanks for this idea!
I love when people share such knowledge and I especially love it when people share their TILs references. That said the tweet and the post kinda feel weird in a way for a non-technical reason: This idea got momentum from some other tweets before and Chris did post it very much shortly after without any reference back saying “Yo I saw this @whatever and I found it to be the best idea ever so I am sharing it” or “My friend just told me he read this in @whatever”.
In our area it is important to acknowledge the references/ideas from others which is why I kinda do not favor the way it got popular on Chris’ account because it felt like he is leaving out where he got the simple but clever “trick” from. IMHO.
(and nope I am not talking bout’ me, just to be clear)
Thanks for sharing though.
I got the trick from a pairing session with a co-worker. If there was some coincidence that made it seem like I “stole” the idea from some other tweet, well, I’m sorry about that, but I didn’t. This technique is based on something that has been in JavaScript for, it looks like about half a decade.
This inspired me to create this:
And call it like:
And everything is pretty-printed :)
An even faster way of doing this is by taking advantage of the functionality that your IDE should provide.
For example, in PyCharm/Webstorm I have a Live Template set up that allows me to to type
clv
, pressTab
and then type the name of my variable and it automatically insertsconsole.log('foo', foo)
.When I’m teaching devs they’re always surprised when they learn about all the coding help they’ve been not using in their IDE of choice.
One thing that frustrates me is when you want do print something on a line along with the object, such as:
console.log(
**** Debug_jwir3: inside some function: ${object)
);In this instance, object doesn’t get expanded (just converted to a string) unless I perform a separate console.log with just the variable.
Use commas instead of string concatenation, such as:
console.log("****Debug_jwir3: inside some function:", object);
And it will render in the same debug output as if it were standalone.
Why not console.log(
myVariable:${myVariable}
);Not very short I guess..
I guess you’d need backticks there to make that work. And yeah the point I think is the brevity.
Hey there,
just learning javascript, and came across this blogpost. Very insightful! And helpfull because I have to console.log every line while learning. This trick helps me differentiate easier which output is from which variable.
How ever when I tried it combined with an array, I came into a bit of trouble. It didnt seem to work?
Any thoughts what I did wrong here:
const colors = ["groen", "geel", "rood", "paars", "blauw", "oranje"];
console.log(colors[4]);
console.log(colors[1]);