Return vs. console.log I cannot seem to understand the differences

function rectangleArea(width, height) {
   console.log(width * height);
}

console.log(rectangleArea(5, 7))

function rectangleArea(width, height) {
  return width * height;
}

console.log(rectangleArea(5,7))

These two renders the same result which is 35. I don’t understand the differences between return and console.log. I know that return only works in function but if console.log can do the same, then, why bother using return?

I am learning JavaScript right now and I am stuck at this topic. Can anyone help explain it, please?

I look at these two lessons and it confused me a lot.

In Parameters and Arguments lesson, the example in the lesson shows using console.log while the Return lesson shows using return. They look the same to me so I dont understand why we are using return.

you say you use different things but you don’t. you’re calling console.log in both cases

if you do the same thing twice then yeah you’ll get the same result both times

they have nothing in common so telling the difference should not be an issue, they’re not alike.

and, if you don’t know why you are using something then you should not be using it. if you have something in your code that you don’t know what it does or has no purpose, then that is a bug.

If you wanted to compare the effects, you’d use console.log in place of return:

function rectangleArea(width, height) {
   console.log(width * height);
}

rectangleArea(5, 7)

function rectangleArea(width, height) {
  return width * height;
}

rectangleArea(5,7)

for which you would get the outcome that one of them prints something… because it calls console.log, which, hey, prints, and the other doesn’t, because you don’t use console.log

if you don’t know what return does then you probably don’t know what a function does. or if you do, then you probably also know what return does even if you might have to think about it for a bit. you could read up on functions here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions or do your own googling… which is generally your first place to look for anything anyhow

Thank you very much! You actually corrected my code and that enlightened me when I tried it. I understand the differences now.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.