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

Miguel Nunez
Miguel Nunez
3,266 Points

Console.log ?

Can some one explain this in a simple way where a normal person can understand what this Java Script code does aka console.log

5 Answers

console.log is considered a debugging tool. You use it to relay messages to you so you know what your code is doing in the background. You use it to help you find and fix a problem if one exists, or keep track of how the code is progressing.

Very basic example of how you can use it to see the progress of your code. For example, if you have 3 functions that run, you can pass a message to yourself to let you know they completed.

firstFunction();
console.log("First function complete");
secondFunction();
console.log("Second function complete");
thirdFunction();
console.log("Third function complete");

So when you test the code above in a browser, you press F12 to open up the console and see if all the functions completed. If they all did, you would see something like:

***First function complete***
***Second function complete***
***Third function complete***

If the results were:

***First function complete***
***Second function complete***

What happened to "Third function complete"? Well, now you know that it didn't complete, and to check the thirdFunction() code. It gives you a starting point of where to look, otherwise you might be trying to find a "needle in a haystack".

Hope that helps!

Simon Coates
Simon Coates
28,694 Points

console log is used to output lines to the javascript console - which is accessible via developer tools. Lines printed to console are not shown to the user under ordinary circumstances. You can use console.log to indicate how the code is executing (eg. console.log("this block executes"); )or to take a look at variables at a particular time (there are more advanced ways to do this). It's for debugging.

Miguel Nunez
Miguel Nunez
3,266 Points

what is the function of the console.log why is it so special and why would I use it ? That's what i'm more concern of.

Simon Coates
Simon Coates
28,694 Points

trivial example:

var x = 6;
var y = 7;
//other code that might modify x goes here.
y++;
y *= 6;

if(x === 6) {
    console.log("x is 6 at this point");
   console.log(y);
} else {
     console.log("running else. x is "+x);
}

To get a quick look at which section of code is being executed (or how many times), or get a quick look at a variable's value. It's the cheap and nasty way to get a look at execution and variables at particular points.

Gi Devs
Gi Devs
12,171 Points

console.log takes a string (or converts to a string) and writes it in the console.

Miguel Nunez
Miguel Nunez
3,266 Points

You guys are not making any since on how a normal person will understand with out using unfamiliar language that people don't understand what is the function of this and how and why I would use it is this code suppose to fix Java codes or can this console.log do more things is that why it's so hard to explain this in simple terms I don't know what strings mean etc... just saying

Simon Coates
Simon Coates
28,694 Points

you can run the code i posted using the developer tools console. It will output

x is 6 at this point
48

You can see the output and think "oh, it ran that line of code" and "oh, the value was 48 on that line". That's it. That's what console.log is for.