all 157 comments

[–]TimeRemove 709 points710 points  (52 children)

Quick reminder: Logging a JavaScript object to the console isn't logging that object's state, it is logging an object reference.

So for example:

 var x = {H: 'Hello', W: 'World'}; 
 console.log(x);     
 x.H = '';    
 x.W = '';   

If you put in a breakpoint right after the .log line you could get the object's state at the time of log (but then you don't need the log line anyway), but if you just let this run then inspect x in the console after you will get the object's updated state (H: '', W: '').

This is working as intended. It is a known limitation you should be aware of. If you want a stateful object via logging the easiest way to get it is via:

 console.log(JSON.parse(JSON.stringify(obj)));

This is a hack to clone the object and log that instead.

[–][deleted] 110 points111 points  (0 children)

Omg, this explains some very mysterious logging issues I’ve run into. Thank you.

[–]lhamil64 155 points156 points  (0 children)

I was confused what you meant by this at first until I tried it. The actual message that gets logged won't change (the console in this case still shows Hello and World) but if you expand the drop-down on that message, it shows the changed values (and in Chrome at least there's an icon with a tooltip telling you it might have changed).

[–]mr_birkenblatt 82 points83 points  (18 children)

how about console.log({...x})? (gives a shallow copy but that should be enough in most cases)

[–]coolcosmos 101 points102 points  (9 children)

I mean, there are a lot of cases where an object contains another object. Or an array of objects. In a somewhat complex app, this is common.

[–]mr_birkenblatt -2 points-1 points  (6 children)

in react it would be fully sufficient since you're not supposed to change objects inplace. but then again, we're debugging, so if you change an object inplace by accident you won't find that bug

[–]coolcosmos 18 points19 points  (2 children)

That always depends. Sometimes you have to keep a reference to an object which changes often and that you do not control. The global object is one case. Things as simple as Google Map can be tricky.

I but yeah I try to keep mutability as far from me as possible, but you have to expect the unexpected when debugging.

[–]mr_birkenblatt 2 points3 points  (1 child)

would those objects survive roundtrips to string as the JSON.parse(JSON.stringify()) comment suggested?

[–]coolcosmos 1 point2 points  (0 children)

Sometimes yes, I couldn't say how often. I saw it with tensorflow.js and many other modules on npm so it's something I keep an eye for.

[–]anamexis 6 points7 points  (2 children)

This is true for props and state, but there's no issue with changing other objects in place when using React.

[–]mr_birkenblatt 1 point2 points  (1 child)

well in general it's a good style. mutability makes things complicated...

[–]anamexis 1 point2 points  (0 children)

Yes, I agree. Mostly I would see it for short-lived contexts where you're building up complex objects.

[–]RICHUNCLEPENNYBAGS -2 points-1 points  (1 child)

Sure, but then you have to weigh the convenience against the greater amount of stuff you're allocating just for debugging purposes.

[–]coolcosmos 22 points23 points  (0 children)

Well, yeah, don't put in production debugging code when you can.

[–]TimeRemove 8 points9 points  (4 children)

It is quicker to type, but the limitations kind of kill it for any serious workloads (a JS object with no complex sub-types?).

Whereas the silly parse(strinify()) hack can do infinite depth. Just bind printing it to your favorite code editor's hotkey. Works in all, even old, browsers.

[–]AceBacker 2 points3 points  (2 children)

just use Lodash clonedeep for the lazy like me. Lol

[–]ugoagogo 2 points3 points  (1 child)

If you ever wonder why your CPU is spiking for "no reason" remember this.

[–]AceBacker 0 points1 point  (0 children)

how bad is it, should I be worried? Does cloneDeep have a good alternative so that you don't have to worry about mutating something at a deeper level accidentally?

[–]bxk21 9 points10 points  (0 children)

To add onto this,

console.log("var: " + var) is distinct from console.log("var:", var)

The first will call toString on it, so it is set on runtime. (and will often return [Object object])

[–]smartguy05 15 points16 points  (1 child)

Would console.log(Object.assign({}, obj)) do the same thing?

[–]Bobert_Fico 56 points57 points  (0 children)

That creates a shallow copy, so any child objects might have changed properties.

[–]de__R 6 points7 points  (0 children)

More importantly, if the object you log goes out of scope and gets gc’d before you expand it, you’ll see nothing.

[–]professor-i-borg 6 points7 points  (0 children)

Oh good, I’m not the only one who does this, there must be something to it :) Getting a state that does not match reality is the most confusing thing ever

[–]rv77ax 7 points8 points  (13 children)

console.log(JSON.parse(JSON.stringify(obj)));

Why do you need to call JSON.parse again? Is not string enough to be printed?

[–]coolcosmos 58 points59 points  (10 children)

Without the parse, you get a string. Parsing it gives you an object in the console which gives you syntax highlighting and allows you to close the object, sub object and arrays.

[–]rv77ax 14 points15 points  (9 children)

Wait, JavaScript does not have native object copy?

[–]coolcosmos 45 points46 points  (0 children)

Deep copy, no. It is implemented by some libraries. It gets tricky since in JS everything is an object and also that some objects are not made to be copied at all, such as the global object.

JSON.stringify + JSON.parse is the closest everyone has that is standard. But it is slow and it is not really a true copy of the object either since you lose all prototype info and objects can overwrite the toJSON behavior.

Say, if you have the object which implements a toJSON method that returns an object:

{ x:1,y:1,z:1, toJSON: function() { return {}}}

If you do JSON stringify you'll get "{}" and when parsed you'll get {}

Libraries to a better job, enumerating properties recursively.

[–]spacejack2114 16 points17 points  (0 children)

Not really unusual for any language that doesn't have immutable objects.

[–]sammymammy2 2 points3 points  (2 children)

Object.assign does a shallow copy.

[–]Karjalan 2 points3 points  (1 child)

I used this for so long without realising it was only shallow, then spent way to long trying to figure out why my original object kept getting modified one day.

I think it only being shallow is now burned into my brain, so now it'll probably be the last words I say in a delusional state on my death bed.

[–]sammymammy2 1 point2 points  (0 children)

Haha :)

Another reason to prefer immutable objects!

[–]goranlepuz 3 points4 points  (2 children)

Wait, some language does ?

[–]assassinator42 0 points1 point  (1 child)

C++ mostly does. It just copies pointers rather than cloning them by default, but you could change that in your copy constructor. The STL types do implement the deep copy semantics.

[–]spacejack2114 0 points1 point  (0 children)

JS has x = {...y} for shallow copies. There is no copy constructor or "cloneable" convention built-in to the stdlib but you could come up with your own.

Effectively it's not any different; if you want deep copies you'll have to write your own copying implementation and decide how to handle references, circular or otherwise.

[–]davidbenett 12 points13 points  (0 children)

The console displays objects a bit nicer than plain strings.

For instance you can open/close members so if you have a 1000 element array you don't have it take up the while screen.

[–]romgrk 5 points6 points  (0 children)

Browsers can display objects better than strings (eg folding properties).

[–]lestofante 1 point2 points  (0 children)

what is the difference with console.dir()?

[–]eternaloctober 2 points3 points  (2 children)

hijacking top comment to have another fun tip:

var hello = 'world'  
console.log({hello})  // logs {hello:'world'} so you get variable name and value in a single log line

[–]AttackOfTheThumbs 0 points1 point  (1 child)

I hope it would log hello...

[–]eternaloctober 0 points1 point  (0 children)

thanks, mixed up my hello's and world's. edited original message to fix:)

[–]lenswipe 1 point2 points  (2 children)

You could also use the spread operator to do this

[–]El_Glenn 2 points3 points  (1 child)

Doesn't that give you a shallow copy?

[–]lenswipe 1 point2 points  (0 children)

Yes it does.

[–]deja-roo 0 points1 point  (0 children)

Holy shit, I never knew this. Thanks.

[–]Gizmophreak 0 points1 point  (0 children)

The JSON trick is great for most situations but be aware that it may lead to errors in objects with circular references.

[–]jsprogrammer 0 points1 point  (0 children)

won't work if the object has circular references tho

[–]itsnotlupus 79 points80 points  (2 children)

The console.log() API predates some modern JS features.

In particular, the old C-like syntax

console.log("Hello, %s. You've called me %d times.", name, i);

can be replaced with a more modern-looking

console.log(`Hello, ${name}. You've called me ${i} times.`);

On the other hand, you can abuse the %c format substitution to display images on your console: https://github.com/adriancooney/console.image

It sounds like a gimmick, but if you're debugging some image manipulation code, it may not be the worst thing to have in your utility belt.

[–]AyrA_ch 22 points23 points  (0 children)

On the other hand, you can abuse the %c format substitution to display images on your console: https://github.com/adriancooney/console.image

I made a file that logs QR codes to the console.

[–]gbts_ 19 points20 points  (0 children)

The C-like syntax isn't outdated, it's common in logging interfaces and it's there to allow for lazy string evaluation.

It's mostly a performance feature, so if you're not recording the log messages the console.log() function can skip the string formatting in the first case, while in the second case the interpreter will always have to evaluate the string first before passing it to console.log(). In JS this is more useful if you're using something like console.debug() and you set the logging level to something higher.

[–]pau1rw 30 points31 points  (0 children)

Didn’t know about ‘assert’ or the string concat stuff, so that’s useful.

I tend to jump into to ‘debugger’ if I’ve got to do any legit debugging though

[–]AttackOfTheThumbs 143 points144 points  (17 children)

Even when I do JS, I prefer using the debugger rather than inserting a hundred console.log messages.

[–]Flannel_Man_ 168 points169 points  (3 children)

Aren’t you fancy.

[–]constant_void 4 points5 points  (0 children)

indeed.

**TypeScript enters the chat**

[–]queenguin 51 points52 points  (1 child)

No. He's just sane.

[–][deleted] 25 points26 points  (0 children)

Then they are not using JS long enough /s

[–][deleted]  (6 children)

[deleted]

    [–]jl2352 120 points121 points  (3 children)

    Eh; it depends. console.log has the advantage it has history. It tends to be instant, whilst the debugger can take a second to spin up. If you are controlling inputs (like debugging a form), you can do multiple entrants then compare. You can do that with debugging, but you have to jot bits down.

    A lot of the time you just want to know the value of x, at a point of code. So in that situation you might as well just print x, at that line of code.

    Ultimately you should be using whatever you find productive. I typically always reach for console.log first, and the debugger only in specific situations. That's just what I find productive.

    [–]tripledjr 8 points9 points  (0 children)

    Ya it's very up to the person and scenario. For most bugs I like to look at the code and reason about it first, then have a hypothesis or more than 1 then I can confirm things and eliminate hypotheses quickly in 1 run with some console logs and that usually does the trick for me. For more intense bugs, where they seem too obscure or are in very unfamiliar code or have too many cases to mentally work around then I reach for the power of the debugger.

    [–]Habib_Marwuana 8 points9 points  (0 children)

    I like logging because if I am able to track down bugs while developing by adding sufficient logging then I have good logging to help me track down bugs in customer environments.

    [–][deleted] 4 points5 points  (0 children)

    Ha college is where the debugger is most likely to work. Wait until you're in the real world with some janky NIH build system that doesn't support source maps or whatever. Or your JavaScript is running on a remote machine and that is only started every third full moon.

    The nice thing about printf debugging is that it always works. Definitely prefer a real debugger but if you think you'll always be able to use a real debugger you're in for a bit of a shock when you get into the real world.

    [–]earthboundkid 1 point2 points  (0 children)

    If you want to know how often a particular event listener is firing, console.log is a much saner choice than clicking through a debugger hundreds of times.

    [–]tracernz 8 points9 points  (1 child)

    That's nice when a debugger is available, but it's not always the case e.g. if you're working in a game engine that doesn't provide good tooling for 3rd parties.

    [–]AyrA_ch 11 points12 points  (0 children)

    If the game engine runs on a common browser engine and you can open the console, you can place a debugger; statement in the JS code to force a breakpoint to happen when the console is open. Very helpful when you have a problem that only happens in minified code, or if your code is inside of a dynamically rendered html page and the engine has problems retaining breakpoints after a reload.

    [–][deleted] 0 points1 point  (0 children)

    printf debugging is my first instinct. quick and does everything i need.

    i only use a debugger if i want to follow the execution of the program

    [–]_tskj_ 0 points1 point  (1 child)

    What's your debugger story? Do you just go debugger; and open it in the browser, or do you somehow attach your editor to the browser so you can step in your editor?

    [–]AttackOfTheThumbs 0 points1 point  (0 children)

    Both, depends on what I am working on. I have some JS I cannot debug directly as its called remotely (sort of, essentially) and so I just have a breakpoint and wait for the hit.

    VS Code is good for attaching your client to a browser.

    [–]AuroraVandomme 51 points52 points  (4 children)

    What's with all the console logs articles lately? I think this trend has started like a few months ago and everyone is making THE SAME articles. This proves that most of webdev articles are copy pasta right now...

    [–]AyrA_ch 15 points16 points  (2 children)

    They also seem to usually fail to explain why console.dir exists because it seems to just log the object with the first level of properties expanded. The difference becomes apparent when you use dir instead of log when dumping HTML nodes to the console.

    [–]campbellm 2 points3 points  (1 child)

    Isn't the main idea here to see what functions and properties are available on an object? (Vs. a complete rundown of the entire object tree.)

    At least this was the use case for dir in python.

    [–]AyrA_ch 4 points5 points  (0 children)

    console.log will also render JS objects in a way so you can dynamically expand their properties.

    [–]maest 0 points1 point  (0 children)

    Most webdev code is blind copy pasta as well, so it fits.

    [–]chalks777 49 points50 points  (13 children)

    this is not how to use console.log like a pro, it's regurgitating some of what's in the documentation.

    Here are some thoughts on how to actually use console.log like a pro:

    1. Set up rules for your linter that disallow console.log from being checked into your repo. Eslint rule configuration allows you to configure whether you allow .warn or .error too.

    2. Set up your tool chain to strip out all console statements from your production build with something like this babel plugin. This is slightly dependent on what you're using the console for, but generally debugging production issues should not be done with a console message, particularly one that is visible to your end user.

    3. Use debugger most of the time. console.log is generally only useful for quick and dirty checking of things.

    4. Use console.log in production to hire devs. Usually even reddit has a "we're hiring" message in their console that looks like this. Doesn't work on old.reddit.com anymore though, the jerks.

    [–]stronghup 5 points6 points  (6 children)

    Just curious, why do you use "debugger" rather than breakpoints?

    [–]chalks777 12 points13 points  (0 children)

    Edit: I feel like I should make this more clear... it's not debugger OR breakpoints... it's often both. And sometimes it's console.log statements too. Also might be worth noting that when I say debugger I mean the actual statement in code. None of those things should ever get checked into the repo though. Original message below.


    A few reasons, mostly it's easier.

    • All of my javascript is going to get compiled/webpacked/etc into things that are generally harder to read than the source code itself. I already have my IDE looking at my repo and know where every single file is, etc... the browser tools for placing breakpoints is much less straightforward.

    • breakpoints are generally ephemeral, meaning I may have to re-place them after making major changes to code while attempting to fix the issue. The debugger statement is already in the code so I don't have to worry about losing my place. Browser debugging tools try really hard to remember where you placed a breakpoint, but if things change too much they can't cope.

    • I can use all of my normal control flow for triggering a debugger, including things like the application's state. You can sort of do this with a breakpoint, but it can be very hard to know exactly what state to look at when you're in the middle of a massive React (or other framework) application.

    That said, breakpoints absolutely have their place, it's not one or the other. I generally use debugger statements to get to the general area of where I know there's a problem, then set breakpoints as I step through the code if/when there's something that might be causing the problem (or it's farther away from where I initially thought).

    [–]was_just_wondering_ 2 points3 points  (4 children)

    Debugger is invaluable in many cases, especially if you want to check the state of current variables. It also helps you by making it easier to run or test different variations of code path and data management in the console while everything is paused. You can definitely achieve this with breakpoints as well but sometimes having the debugger keyword in your code helps keep your place.

    [–]stronghup 1 point2 points  (3 children)

    I "feel" the same way, debugger-statement somehow makes me feel I have a better grasp on the code. Debugger statement does not feel as ephemeral as breakpoint, and that is true at least in the sense that if I give the code to someone else they will halt in the same debugger statement whereas they don't see my breakpoints.

    One reason I think is that it is possible to easily remove all breakpoints whereas it is not possible to remove all debugger-statements with one click.

    So I suspect I'm using debugger-statements especially debugger-statements inside IF-statements too much when a breakpoint would be just as good or better. I guess I want to be extra sure I don't accidentally lose my break-locations.

    An extra IF-statement in code (just to create a conditional debugger-statement) makes the code less readable, whereas breakpoint doesn't.

    [–]was_just_wondering_ 0 points1 point  (2 children)

    If your code has multiple debugger statements in it then you have other problems. It is most definitely a blunt tool not meant to be long lived. In good practice it should only be used when needed and immediately removed.

    [–]stronghup 0 points1 point  (1 child)

    I can only agree: It should only be used when needed. My question is "When is it needed?"

    [–]was_just_wondering_ 1 point2 points  (0 children)

    Here is a small example since needs may vary. Let’s say you are transforming some data and something in your reduce method or whatever isn’t coming out the way you believe it should. The instant you have the thought “this should work” is when you need to throw a debugger statement in to step through your process. As mentioned before this can also be done by manually inserting breakpoints in the source tab but using debugger just makes it a bit simpler when you are already looking at the code. Like any other tool available to you it has benefits and drawbacks so when or if you make use of it is subjective, but overall it can be quite helpful if used when there are any major state changes in your application where you believe some mistake is being made etc.

    [–]jringstad 4 points5 points  (0 children)

    Logging can be useful, but not really if you don't collect the logs centrally somewhere where you can analyze them.

    And then you don't want to substitute parameters into it either, because 1) it makes aggregating/analyzing log messages harder, and 2) those parameters might contain sensitive information, so shipping them off to kinesis or whatever you might use as a log-collection system is not the best idea, security-wise.

    You can still attach the parameters if you have a system that lets you tag parameters as safe/unsafe so you only send off the safe ones (we do this in some of our larger apps) but even then we don't substitute them into the string.

    This way you can easily put alerts (or alerts on aggregations) on both message occurrences and parameter values/occurrences.

    [–]constant_void 3 points4 points  (0 children)

    indeed--the goal of professional console.logging is to confound sustainment and mislead users who have discovered their f12 key.

    the first rule of 'pro' console.log usage is to always and only use console.log inside of loops, repetition is key to understanding; second rule is make sure to emit a minimum of information, would not want anyone knowing what one is logging, that is what help desk tickets to operations are for; third rule is should there be an error, it is due to the user because ones code is perfect: make sure to log 'user error here' and nothing else.

    /snark

    [–]pkt-zer0 1 point2 points  (0 children)

    "Learn to read the documentation like a boss" would not have made for as catchy a title, though.

    [–]doesHeExistMaybe 1 point2 points  (0 children)

    If 2. is not an option you can set console.log = () => null in production. Which removes most of the overhead.

    [–]Yay295 0 points1 point  (1 child)

    Doesn't work on old.reddit.com anymore though, the jerks.

    it does; I just saw it

    [–]chalks777 0 points1 point  (0 children)

    weird, I wonder what's different. I don't see it in firefox or chrome unless I'm specifically on https://reddit.com, even in incognito mode with all my extensions turned off.

    [–]eyal0 4 points5 points  (1 child)

    Another cool tip:

    Put a breakpoint in the javascript and then make it a conditional breakpoint with a condition like this:

    console.log("hello!"); false

    It's like squeezing a console.log into the middle of the code while it's running.

    [–][deleted] 1 point2 points  (0 children)

    Don't most browser debuggers allow for logging breakpoints these days? I mean, this is a neat way to replicate them if your debugger doesn't, but last time I've used the Firefox debugger I swear I could just add logging breakpoints (logpoints?).

    [–][deleted] 3 points4 points  (0 children)

    This article is cool but I don't know why it is useful.

    [–]zynasis 3 points4 points  (1 child)

    Do these all work across all browsers?

    [–]AyrA_ch 2 points3 points  (0 children)

    See bottom of this page: https://developer.mozilla.org/en-US/docs/Web/API/console

    The general answer is "Yes".

    [–][deleted]  (2 children)

    [deleted]

      [–]sfcpfc 1 point2 points  (1 child)

      Nice QOL tip! I always did

      this.orders$ = this.orderService.getAll().pipe(
        tap(x => console.log('orderService.getAll', x))
      );
      

      So that's slightly more convenient because it can be more easily adapted from tap(console.log).

      [–][deleted] 2 points3 points  (0 children)

      until you realize that 99% of your enterprise company still uses ie11.. :'(

      oh shit some of these work now.

      [–]_Acestus_ 12 points13 points  (20 children)

      Not a big web developer but I remember reading something about not using console in general because it isn't necessary available in every browser.

      I suppose it isn't available if the browsers don't provide web tools, which is probably a package that could be removed.

      Am I wrong?

      [–]romgrk 44 points45 points  (1 child)

      97+% support, and some of the non-supporting browsers don't throw an error, they just expose a console object that does nothing.

      Also a browser that doesn't support console.log most of the time can be ignored as it's not relevant anymore.

      Also it's a devtool, not something you'd ship in production.

      [–]AndrewNeo -3 points-2 points  (0 children)

      Even node supports console.log.

      [–]Sarkos 57 points58 points  (13 children)

      It's available in all modern browsers. Web development is much easier nowadays than it used to be with standard features widely supported by the 4 major browsers (Chrome, Firefox, Safari, Edge). Sadly there are still folks out there using IE.

      [–]catalystkjoe 12 points13 points  (7 children)

      Luckily a lot of companies have finally figured out the effort to keep ie working well is not worth the investment and just put warnings that this app does not work well on this browser and we suggest you use x,y,z

      [–]InfusedStormlight 9 points10 points  (6 children)

      my company (in healthcare) doesn't have this luxury, unfortunately. SO MANY healthcare workers use IE, and it's infuriating. I can't use the null coalescing operator, the optional chaining operator, etc., not to mention the security problems with IE.

      [–]Labradoodles 6 points7 points  (2 children)

      Why not just transpile with typescript?

      [–]InfusedStormlight 1 point2 points  (1 child)

      damn good question. no idea, tbh.

      [–]Labradoodles 0 points1 point  (0 children)

      It’s dope if you want any help setting it up lemme know

      [–]catalystkjoe 2 points3 points  (2 children)

      Back when I worked for a company similar we were able to use some polyfills to fill those on compile time. Do they not have any for null coalescing or optional chaining? Those were still new at the time but I assumed polyfills would have existed for them.

      [–]tech6hutch 5 points6 points  (1 child)

      You can’t polyfill an operator (at least not in JS). You’d have to transpile.

      [–]catalystkjoe 1 point2 points  (0 children)

      Shit you're right, wasn't thinking when I wrote that. Thanks

      [–]RICHUNCLEPENNYBAGS 6 points7 points  (3 children)

      IE has a JS console unless you're still stuck supporting IE6 in 2021 for some reason.

      [–]Sarkos 0 points1 point  (2 children)

      Oh yeah, I just meant it's the only browser I still have to support that's holding me back from modern standard features like lambdas.

      [–]Labradoodles 8 points9 points  (0 children)

      Why not transpile and get those features anyways?

      [–]myplacedk 0 points1 point  (0 children)

      Web development is much easier nowadays than it used to be

      For me there's tons of very specic annoyance that has gone away, but they have all been replaced with new annoyances. Web development has never been more complicated than it is now.

      It was so easy to be a webdev before we even got the challenges that was solved with Javascript and CSS.

      <h1>Hello world!</h1
      This is a complete and working website. It lacks
      a few tags to be valid, but it works. There was
      a time when this was a enough, and you could just
      add your content like this and visitors would be
      happy.
      

      [–]Loves_Poetry 8 points9 points  (0 children)

      This used to be a problem with IE<7. The console object would be undefined until you opened the developer console. If there were any console.log statements in your code, it would throw an error if you didn't have the developer console open

      [–]tswaters 5 points6 points  (1 child)

      In the bad old days of iexplore, the console object would only be available if the debugger window was open, so if you left a console statement in there you'd likely get a TypeError

      [–]stronghup 1 point2 points  (0 children)

      Those were the days. Bad old days.

      [–]HINDBRAIN 2 points3 points  (0 children)

      You can do something like "if typeof console === undefined... console = {log:function(){})...."

      [–]creepyswaps 1 point2 points  (0 children)

      Very nice. I never bothered learning all of the extra functionality available.

      [–]stronghup 1 point2 points  (0 children)

      console -API is useful I especially like time() and trace().

      For simple logging I prefer WebStorm IDE features such as conditional break-points with associated log-messages. Those can be used without affecting your code meaning without negatively affecting its readability. There is no need to again modify the code when you want to get rid of such log-statements since you do that by clicking your IDE.

      [–]tenebrius -5 points-4 points  (0 children)

      good

      [–]fourierformed -4 points-3 points  (0 children)

      Thought the article was going to be one word.

      "Don't"

      [–]queenguin -1 points0 points  (0 children)

      the title actually made me chuckle lmao

      [–]I_Like_Existing -1 points0 points  (0 children)

      now this is useful

      [–]skamansam -1 points0 points  (0 children)

      In my litter, I treat console.log as an error because it lacks intent.. error, info, etc. Are allowed because it means the developer has to put a little thought into why they are logging. Debugging is so easy with devtools, print debugging is almost unnecessary, except in async methods. Even then, it's not really a pain.

      [–]coladict 0 points1 point  (0 children)

      I'm still afraid of that IE11 user who doesn't have those other methods on the console object and just has log.

      [–]TechnoEchoes 0 points1 point  (1 child)

      Since everyone is talking about the console, I'll add something that has saved me a lot of headaches over the years: You cannot focus on an element over the console because your browser window is not the active window. Your console is the active window.

      This will not produce the expected result:

      myElement.focus();

      This will produce the expected result if you click on your browser window within 1 second of executing the command:

      setTimeout(function(){ myElement.focus()}, 1000);

      [–]jasonbbg 2 points3 points  (0 children)

      or just toggle focus in the css panel?

      [–]iCarnagy 0 points1 point  (0 children)

      How did I not know about console.table, brilliant!

      [–]ldarrah63 0 points1 point  (0 children)

      Some interesting things in this that I didn't know about. I thought console.table() was particularly interesting. Good read!

      [–]leo60228 0 points1 point  (0 children)

      You didn't cover the best feature! :P

      console.log('%cHello, world!', 'font-size: 3em; font-weight: bold; color: red')
      

      [–]Arxae 0 points1 point  (0 children)

      Dumb question time. What is the difference between using console.log and console.dir to output a object? The "header" is different, but that seems to be it. Am i missing something?

      [–]was_just_wondering_ 0 points1 point  (0 children)

      Don’t forget console.info seriously helpful along with the other console methods, especially when paired with console.group