As The last ship sailed towards the distant horizon I sat there watching on a rock My mind slowly drifting away Forming into my... Dreamtale
Javascript debugging tips
2018-04-10 / 3 min read

From https://medium.com/appsflyer/10-tips-for-javascript-debugging-like-a-pro-with-console-7140027eb5f6

  • console.trace()
    If you want to know where the log is being prompted from use console.trace() to get the stack trace with the logged data.

  • console.time() and console.timeEnd()
    If you are trying to find a sneaky performance issue, start counting time with console.time() and print with console.timeEnd().

  • console.memory
    If your performance issue is even trickier, and you are looking for a sneaky memory leak, you might like to try and utilize console.memory (property, not a function) to check out your heap size status.

  • console.profile(‘profileName’) and console.profileEnd(‘profileName’)

This is not standard, but is widely supported. You can start and end a browser performance tool - performance profile from the code using console.profile(‘profileName’) and then console.profileEnd(‘profileName’). This will help you profile EXACTLY what you want, and prevents you from having to be mouse-click, timing dependent.

  • console.count(“STUFF I COUNT”)
    In a case of recurring function or code, you can use console.count(‘?’) to keep count of how many times your code is read.

  • console.assert(false, “Log me!”)
    Yes, conditional logging without wrapping your logs with if-else 😃
    You can use console.assert(condition, msg) to log something when the condition is falsy.
    *disclaimer — in Node.js this will throw Assertion Error!

  • console.group(‘group’) ** and ** console.groupEnd(‘group’)
    After writing so many logs, you might want to organize them. A small and useful tool for that is the console.group() & console.groupEnd(). Using console group, your console logs are grouped together, while each grouping creates another level in the hierarchy. Calling groupEnd reduces one.

  • String substitutions
    When logging, you can incorporate variables using string substitutions. These references should be types (%s = string, %i = integer, %o = object, %f = float).

  • console.clear()
    Well, having written so many logs, it’s now time to clear your console a little.

  • console.table()