Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Matt Parry
Added: Aug 12, 2018 7:25 PM
Views: 4
Tags: no tags
https://medium.com/appsflyer/10-tips-for-javascript-debugging-like-a-pro-with-console-7140027eb5f6
  1. Tip #1 console.trace()
  2.  
  3. If you want to know where the log is being prompted from use console.trace() to get the stack trace with the logged data.
  4.  
  5.  
  6. Tip #2 console.time() && console.timeEnd()
  7.  
  8. If you are trying to find a sneaky performance issue, start counting time with console.time() and print with console.timeEnd().
  9.  
  10.  
  11. Tip #3 console.memory
  12.  
  13. 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.
  14.  
  15.  
  16. Tip #4 console.profile(‘profileName’) & console.profileEnd(‘profileName’)
  17.  
  18. 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.
  19.  
  20. Tip #5 console.count(“STUFF I COUNT”)
  21.  
  22. In a case of recurring function or code, you can use console.count(?) to keep count of how many times your code is read.
  23.  
  24.  
  25. Tip #6 console.assert(false, “Log me!)
  26.  
  27. Yes, conditional logging without wrapping your logs with if-else :)
  28. You can use console.assert(condition, msg) to log something when the condition is falsy.
  29. *disclaimer — in Node.js this will throw Assertion Error!
  30.  
  31.  
  32. Tip #7 console.group(‘group’) & console.groupEnd(‘group’)
  33.  
  34. 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.
  35.  
  36.  
  37. Tip #8 String substitutions
  38.  
  39. When logging, you can incorporate variables using string substitutions. These references should be types (%s = string, %i = integer, %o = object, %f = float).
  40.  
  41.  
  42. Tip #9 console.clear()
  43.  
  44. Well, having written so many logs, it’s now time to clear your console a little.
  45.  
  46.  
  47. Tip #10 console.table()
  48.  
  49. Saving the best for last, this is a true gem in my opinion! You can actually print a very nice table with the objects you log using the console.table()
  50.