Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

console.log(), return, and throw

Is it true to assume that console.log can just gives a (custom error, or any other possible output), while throw stops a function and give a console error, and return just stops a function?

Did I describe the 3 in an accurate way and that's all the difference?

1 Answer

Steven Parker
Steven Parker
243,318 Points

You're pretty close.

I would say that console.log is for just general output. There's also a console.error specifically for error output which is normally shown in a different color.

And throw does stop a function but that's not it's primary purpose. It can also stop the whole program if not "caught". Yet by itself it does not create any output. That can be easily done when you catch it.

Finally, return does stop a function, and can optionally return a value to the caller of the function.

Return does not stop a function? It seems to contradict what I once learned about Returns. Maybe you meant that in some extraordinary cases, it doesn't stop it but usually is?

About try-catch and throw, is it true that to understand try-catch properly, one must first have an understanding of the "throw" concept by itself?

Steven Parker
Steven Parker
243,318 Points

I think you misread my line. I said "return does stop a function". It can just stop it, or it can stop it and also pass back a value.

You can use try-catch successfully for error handling without ever using an actual "throw" statement. But you should understand what an exception is.

Yes, I misread it. Regarding exceptions. Of course I know the general sense and if I translate it to Javascript it's:

Try, if didn't go (if you didn't catch), display (maybe via a throw) an exception.