If you’re a software engineer, or even if you code casually, you are very familiar with the beloved console.log() function. It’s one of the most handy tools we have in our belt to quickly test if our code is working and debug it when it’s not. Up until recently, I was only aware of console.log() as a way to work in my console.
Calling console.log() simply prints out what you put in as parameters (between the parentheses). Console.log() works with any variables that have been assigned values, functions, integers, strings, booleans, and everything in between. It’s especially helpful to use when you’re writing functions during the development stage of a project — logging something inside a function is a really quick way to see if your function is running the way you’d like it to.
It turns out your console has a number of other functions that help you to work through your code. A console function that I learned about recently which is very similar to console.log() is console.error(). Console.error() does essentially the same thing as logging a message in your console, with the exception of the way that your log is displayed in the console. As you can see in the example above, each time I logged something in the console it showed up as a normal line. With console.error(), your information is displayed as an error message.
This function can be especially helpful when you’re in the debugging stage of a project. As developers we want to be as descriptive with our code as we can, for ours and others benefits. You can imagine that during the debugging process, a red error message showing up in the console would send a clearer message to yourself and other developers than console.logging “this is an error,” which could easily go unnoticed.
As an extra helpful hint, the console also includes a console.warn() function — it’s essentially the same thing as console.error() but with a different display.
Again, this can come in handy when debugging or even in development. After learning about these specific console functions I’m going to try and incorporate them into my code more, so that I can be more descriptive in my work and hopefully help out others who are going to be looking at and working on my code.
This last one is really simple, but incredibly helpful to those of us who can’t stand clutter, in life or on our screens. You’ve flooded your console with all of your neat new console functions that you learned about, and now there’s too much to look at. Our console is nice enough to include the console.clear() function, which can also be called as clear(). This will wipe out everything in the console and leave us with a nice message saying that the console has been cleared.
Hopefully these console functions have enlightened you a bit, and will help you in your code going forward. There are actually a lot more functions available to you in the console. Here’s a great article going over more of the console functions in a bit more detail. Happy coding!