Function Syntax question -- Basics Section 8 Type of Functions

In this section we create that basic squaring function.

I tried it this way and it works, so my question is aside from it being less clean, is there anything illegal/potentially troublesome with it?

Code from course:
function square(number) {

return number*number;

}

console.log(square(2));

Code I came up with:
function square1(number1) {
number1 = number1 * number1;

console.log(number1);

}

square1(4);

Hi,

Depends on the purpose actually.
But generally speaking Mosh’s approach is more modular.
Because it does one thing : returning the result for a square.

function square(number) {
  return number*number;
}

console.log(square(2));

Yours on the other hand includes other concerns such as displaying the result which is not necessarily a good idea.

But sometimes it can be.

function square1(number1) {
  number1 = number1 * number1;
  console.log(number1);
}

square1(4);

Basically I would start Mosh’s way. Then if I would need to display this often in the console I would base on it writing a new function.

function displaySquare(number2){
  console.log(square(number2));
}

displaySquare(4);

Because backstory : There is another piece of code using the square() function out there and we don’t want any display in the console. This is unwanted to display in the console (outside of personal quick tests). Would you want a production code to reveal sensitive data because you forgot to remove console.log anywhere?

Say We would have started your way and we need a function that does not need the console.log.
On another hand we have another piece of code that breaks without it.
What should we do?
It looks easy peasy on a very simple example. But on big projects doing what seems obvious (remove the console.log from the method then create a new one such as displaySquare()) isn’t actually so obvious (your code-base may be too big and it could have potential impact on any other piece of code you didn’t know exist). Just replace console.log with any operation that could actually be problematic

I’m not that much into JS myself but I heard console.log is not a good practice. JS developers have other ways to tryout things.

A bit of reading if you want to know more on the topic.

Cheers.