Bad form in the 'Return' lesson?

In the ‘Return’ lesson in the JavaScript course, I think it may teach bad form. The lesson requires you to use a const called numOfMonitors, which needs to be assigned the value monitorCount(5,4). However, I’m pretty sure it’s considered bad form to create a new variable anywhere other than at the top of the program or loop, and creating a const at the top would require hoisting, which is also considered bad form. A better way to accomplish the same task would be to create an empty let at the top of the code and assign it later, as seen here:

`let numOfMonitors;
function monitorCount(rows, columns){
return rows * columns;
}

numOfMonitors = monitorCount(5, 4);
console.log(numOfMonitors);`

The code is completely functional, and doesn’t require hoisting or creating a new variable in the middle of the code. I could be wrong about this being improper form (I first learned JavaScript before they added let and const,) but I’m pretty sure that didn’t change.

Hey, can you paste the full bad form example? (and you can format it by selecting the text and pressing the </> button).

“Good practice” is always a guiding principle. It is contextual and acts as a good starting point, but it is not the end itself… And as much as I love form, I don’t defend anything I don’t understand clearly, a lot of the bending and breaking points make for great learning foundations (all of this is just theoretical musing not aimed at you or the example!)

Just curious as to what the code you’re alluding to looks like. :sweat_smile:
And if you know a reference in documentation to this bad practice feel free to share it too! All this for the spirit of discussion.

1 Like

Here is the code it wants:

function monitorCount(rows, columns){

return rows * columns;

}

const numOfMonitors = monitorCount(5, 4);

console.log(numOfMonitors);

Ah, yea. Hm.

In my opinion in a larger program this structure sometimes makes sense (especially for readability). But it depends on context. If there’s a lot of files I feel like it would look more like the given example. If it’s a single file the way you wrote it is also logical.

As I said in the beginning, when I learned JavaScript for the first time, the const and let types didn’t exist-- there was only var. A lot could have changed since then.

1 Like