Proper use of console.assert in JavaScript

Share this video with your friends

Send Tweet
Published 7 years ago
Updated 4 years ago

Learn about console.assert, which is syntactic sugar for logging an error the console when a given condition is not met. It's useful, but may not do what you expect if you're coming from another language - watch this lesson to learn how to use it, and when not to.

I'm going to introduce a method called console.assert, but before I do, I'd like to explain to you why you should be very careful using this method. What this is going to do is print an error to the console if some condition is not met.

All of that logic, all of the handling and determining whether the condition is met, is actually just built right into the assert method on the console object, meaning that it's going to log an error to the console, and your code is just going to keep running.

This is not actually catching the error. This is not handling the error. You should not understand using this method to be some sort of alternative or easy way around handling errors in your application. I'll show you what I mean.

Let's say that we want to write a function that counts the length of a string. It's going to tell us how many characters there are in a string. We've got this constraint. The string that comes in as an input, we expect it to be at least 20 characters long.

I don't know why. Don't worry about the domain here. I'm just trying to create a simple problem for you. One way to do this would be to say if str.length is less than 20, then we could throw new error. The string was too short.

Else, or even just here, we could say return str.length. I can say count string characters. This is a very long string that should work, and everything is fine. Great. Count string characters, hi. Uncaught error. We get our little exception here.

What we can do with console.assert is rewrite this function to say count string characters. We can say console.assert str.length is greater than or equal to 20. Otherwise, print the error, the string was too short. Then we return str.length.

That might be a little bit shorter and easier to write, right? Totally. No problem. Do you see what the problem is here? If I say, let's just go ahead and call this on our long input. That returns 67, as expected, but this one, we get our little error because our assertion failed, but it also returned the value.

Console.assert, this is not like an assert in Java, where if the assert fails, the frickin' thread craps out. This is not actually generating any kind of a meaningful exception. All this is doing is giving you a way to log an error to the console in the event that some condition fails, but then the rest of your code goes on its merry way.

That's why I'm wrapping in this a huge set of asterisks, where yeah, OK, it's a cool convenience method, but don't be fooled. This is not going to break the flow of control. This is not going to prevent bogus data from propagating through your application state.

Nishchal Gautam
Nishchal Gautam
~ 6 years ago

BTW, console.assert is very useful if you're doing a quick TDD when you're writing on JSBIN or something like that. So you might want to do something like:


console.assert(myFn(3) === 3);
console.assert(myFn(6) === 3);"All tests passed");

then implement the function so that there are no errors in console and then finally delete the console calls.