Lesson 11 - why do i need console.log?


#1

Hi everyone.

I just wonder, what I need the console.log operation for in this exercise? I initially forgot it, but the code returns the correct string anyway, just by calling the function. Codecademy still tells me its wrong since I did not use console.log to print the result.

But do I really need console.log in this exercise, since the function itself already prints the result?


#2

@mrtony

Please post your code right here so we can understand your problem.


#3

If your function doesn't contain code to print something, then it did not print that something. Something else did.

So, if you are to print something, well, you'll need code which does that, right


#4

The problem is not about my code, its about what the exercise asks me to to.

First it asks me to create a function in which in use "return" to return a string with the name as a variable in it.

Then it asks me to print this string using the "console.log" command.

BUT, using "return" inside my function will automaticly make it print the mentioned string on my screen. Which is why I wonder, why I hould use the "console.log" command to print it out - since it already does that.

Here you go:

var nameString = function (name) {
	return "Hi, I am" +" "+name;


};
nameString("Tony")
console.log();

As you can see i had to put the console.log command in empty, just to satisfy the codecademy exercise. If i remove it, everything works as it should. The text gets printed on my screen with the name in it. So why does it want me to use an additional console.log command?


#5

You have two ways to do this:

  1. You just put nameString("Tony") inside the console.log(), so it becomes console.log(nameString("Tony"). Whenever you have a function of this sort, you have to read it from the inside: first it calls nameString and then it logs the result of that call using console.log.
  2. You declare a variable, which you then pass to console.log:

var greeting = nameString("Tony");
console.log(greeting);

#6

Thank you but still:
If I dont use console.log and just let the function do what it does, it will still print/Show the resuelt on screen, since it uses the "return" command in it.

So why do i need an additional console.log command?


#7

So what you're saying is that this will print the text?

var nameString = function (name) {
	return "Hi, I am" +" "+name;


};
nameString("Tony")

It doesn't. Try running it somewhere. return gives you the value, to do something with it, it doesn't specify what. If you don't want it to be printed, it won't be, but if you do, then you'll have to specify that by using console.log for instance.


#8

I did. In my first try, i did not read the part about "use console.log to print out..." so I lust left it out. When I hit "save/submit code" the preview window showed the string with my name in the correct place. Which IS the reason I started wondering about the additional console.log requirement.

Or is this just an issue with the codeacademy editor?


#9

This is a typical JavaScript thing where it evaluates your last expression and logs it to the console for debugging reasons.