Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Can anyone explain to a beginner what console.log does?

Hi all! I'm taking the beginning parts of the JavaScript Track, and I'm confused about what console.log does. I think I understand that console.log stores the data that the person enters, but where? An example:

var visitorName = prompt("what is your name?");
//alert (visitorName);
console.log(visitorName);

3 Answers

Hi,

console.log() is a function that prints to the terminal (or console) whatever parameter it's been given. It doesn't store information so much as simply take in data and print it.

In your example, what's happening is that the builtin JavaScript function 'prompt' is asking the user for their name, and then whatever the user inputs is stored in the variable 'visitorName'. You're then asking console.log to print out the contents of that variable.

You can also use console.log to print directly (without using a variable), like:

console.log("Here's my string!");

Hope that helps clarify a bit.

Cena Mayo's explanation is pretty solid. I would just add, that most people use console.log() to check and see if their code is working. Like in the example you have, you are alerting the variable visitorName, and you are printing the variable to the console. they should both match up. this is one way to know that things are working as they should.

Nicholas Grenwalt
Nicholas Grenwalt
46,626 Points

Got to love writing a comment at pretty much the same time as another user that says practically the same thing. haha You beat me this time Jacob! ;)

P.S. How do you go about becoming a moderator if you don't mind me asking?

Hey man, great minds think alike!

I'm not sure about others but my experience was that someone, I don't know who recommended me to be a moderator. Then I was asked by Treehouse to be a moderator. That's all she wrote. Is that something you're interested in?

I don't know if I was recommended (as Jacob says), but I was invited by Treehouse. I was at about 40000 points at the time, but I've seen moderators with as few as 3k, so that doesn't appear to factor into things much. My working theory is that it's based on a)need, b)answering questions in the forum and possibly c) being recommended.

Nicholas Grenwalt
Nicholas Grenwalt
46,626 Points

Most definitely. If you could put a word in for me somehow I'd greatly appreciate it. I love the Treehouse community and am actively helping others in the community pretty much daily. I've learned so much from this site and I think I represent the brand well.

The reason I put that I was recommended was due to moderators having the ability to recommend people, but honestly I don't know. I think its mostly about need/really wanting to help others learn.

  1. learn
  2. do.
  3. teach.
Nicholas Grenwalt
Nicholas Grenwalt
46,626 Points

I understand. Sounds like it isn't a really defined process. I guess I'll just keep chugging away at answering questions until my day comes right? haha Thank you both for your time guys. I greatly appreciate it.

Nicholas Grenwalt
Nicholas Grenwalt
46,626 Points

To add to Cena's great explanation, the console isn't something that gets displayed to the web page for a user to see, but is rather a tool used primarily by developers for testing to make sure that sections of their code are running as they had planned.