purpose of console.log

Get help with Code for Teens
matermultorum
Posts: 2
Joined: Tue May 15, 2018 12:31 pm

purpose of console.log

Post by matermultorum » Thu Jun 14, 2018 12:16 pm

I'm having trouble understanding the usefulness of console.log. The text mentions displaying typed out info and I can see that it functions differently than comments, but what does it do that comments don't? Maybe I don't understand what logging a message into the console means. Does it have benefits at a higher level that aren't as obvious at the level we are at? I also noticed that I get similar results no matter where I put the console.log statement in the following lines. When I invoke the function it says 678, when I switch console and the function I get 678 undefined (see below)

function iDoNot(){
return 678
}
undefined
console.log(iDoNot());
678
undefined
iDoNot(console.log);
678

Thanks in advance for looking at this!
Kendall

JeremyMoritz
Site Admin
Posts: 8
Joined: Mon May 14, 2018 10:19 pm
Location: Kansas
Contact:

Re: purpose of console.log

Post by JeremyMoritz » Fri Jun 15, 2018 12:13 pm

Remember that console.log() is a function. Therefore, you could put logic inside it. If you put a comment

Code: Select all

// 6 + 5
You'll only see that. But if you use a function

Code: Select all

console.log(6+5);
It will log the result to the console.

The iDoNot() function you created here doesn't expect any arguments, so passing in the console.log (as you did in your last example) is just the same as calling it without passing anything into it.

If you don't fully understand this yet, just hang in there. The issue is only because these examples have been overly simplified. It will all make since in time, and you'll see the significant distinctions among comments, return statements of custom functions, and the console.log() function.
Jeremy Moritz
Author of Code for Teens
www.CodeForTeens.com

Post Reply