Skip to main content

Make use of JavaScript console methods

When I say JavaScript console methods, the one and only thing that hit most of our mind is console.log() and we use it a lot and only that some might be familiar with error() and debug(). Did you ever think of checking if there are any other methods available in console other than log? Most of us don't do that, but knowing those methods might have saved lot of our development time. In this post I will show you some of the console methods which will be very useful in our day to day coding.

Log based on condition

console.assert(assertion, log_message);
To print something on the console only if the assertion failed - assertion can be any expression which returns a boolean.
Note: In chrome this log will print as an error with message saying "Assertion failed:" with the log_message passed. In FireFox its a normal log message.

Log number of occurrence 

console.count([label]);
If we want to count something, it may be a click, may be a callback, or event triggers. We don't need to create a var and increment and then log. We can simply use console.count('my_clicks') and console will do the counts for you and print the same. Wow! Nice right? But see below for another handy method which we missed.

Log time taken

console.time([label]);
//do_some_work
console.timeEnd([label]);
An easy way for developers to see how much time a logic is running. Given a label name this method will print the amount of time taken in milliseconds between the time() and timeEnd() method for that particular label.


Trace who triggered this function

console.trace();
Did you ever care to find which action is triggering a particular action or event? If yes, trace() is a wonderful console method which will give you all the levels of trace till the line where you called trace() method. This will give the file names with line numbers where the trigger came from. A big time saver right?

Do you know that you can use c like syntax in console messages?

var test = ['one','two']
console.log("This is an object %o and its length is %d", test, test.length);

List of substitutions you can use:

  • %o or %O : Outputs a JavaScript object. Clicking the object name opens more information about it in the inspector.
  • %d or %i   : Outputs an integer. Number formatting is supported, for example  console.log("Foo %.2d", 1.1) will output the number as two significant figures with a leading 0: Foo 01
  • %s : Outputs a string.
  • %f  : Outputs a floating-point value. Formatting is supported.  
If you find some other useful methods, let me know in the comments section and check the source link for some more methods, details and browser support.

Source: https://developer.mozilla.org/en-US/docs/Web/API/console

Comments

Post a Comment

Popular posts from this blog

Upgrade your kitchen with latest Tech Gadgets and Make cooking faster

Welcome to our latest blog post, where we embark on an exciting culinary journey to elevate your kitchen experience with cutting-edge technology gadgets. In this fast-paced world, cooking has become more than just a necessity; it's an art form that deserves the finest tools. Join us as we explore the innovative tech solutions that can revolutionize the heart of your home, making cooking not only faster but also more enjoyable and efficient. From smart appliances to intelligent cooking assistants, we'll introduce you to a range of futuristic kitchen companions that are sure to inspire your inner chef and transform the way you create delightful meals for yourself and your loved ones. Get ready to upgrade your kitchen and embrace the future of cooking!  In this blog post, we will narrow down the vast array of available kitchen tech gadgets and present only 10 items, carefully selected based on product ratings, price, and their suitability for Indian kitchens.  Are you a Telegram u

TataSky Refresh after Recharge - Activate after Recharge

If you are using TataSky and doing recharge occasionally and that too after disconnection, this post will be very helpful for you. Most of the time you will not get the channels listed as soon as you make the payment. It will display message to Subscribe for that channel. You can easily get channels back by simply giving a missed a call from your registered mobile number. Note: Make sure your TV and SetupBox is on and showing the error message Give missed call to  +91 80892 80892 (Soft Refresh)   wait for 1 minute and If still not active then try giving missed call to  +91 90405 90405 (Heavy Refresh). Ad: Planning to buy a Best low budget Smart TV? Consider  Acer 109 cm (43 inches) I Series 4K Ultra HD Android Smart LED TV AR43AR2851UDFL (Black)  - You can get this TV as low as for 20,000 Rs - which has Bluetooth, WiFi, Android and good customer ratings (4.4/5). Note: Price based on offers, click on the link to see current price on the web page If the above ste

Virtual Machine Storage - Fixed Size vs Dynamic Size

Virtual Machine Storage type Fixed size or Dynamic size? Which one Should I use? Whoever used a virtual machine in your lifetime you might have came across this prompt while creating a virtual machine.  You should have seen the option to choose from either Dynamically Allocated  Fixed Size And its obvious that many go for the default one Dynamically allocated.  Did you ever thought what is the advantage of Fixed size over Dynamic size? Here is where the difference I noticed Dynamically allocated This type of virtual disk file will not consume the actual physical storage in your hard disk but it shows as it has that many allocated size (free memory) available to your virtual OS. The advantages are: Actual Physical memory will not be occupied You can actually create a virtual disk even more than the physical drive size eg: you can create 1 TB virtual disk on a 128 GB physical storage. But here is a big drawback which many failed to consider. Since y