Use of let and const variable for functions

In the course examples about functions, the let and const variable are used sometimes.


function rectangleArea(width, height) {
  let area = width * height;
}
console.log(rectangleArea(5, 7)) // Prints undefined

This example uses the let variable.

image

But in the exercise solution for the 2nd step these variables are not used.

function monitorCount (rows, columns) {

return rows * columns;

}

This is the link to the exercise

https://www.codecademy.com/courses/introduction-to-javascript/lessons/functions/exercises/return

Could someone explain when const or let variables should be used regarding functions please?

I think that in this exercise, it’s just a personal preference.

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

does the same thing as

function monitorCount (rows, columns) {
  const count = rows * columns;
  return count;
}

However, since I only ever use count once (and its a number), I guess its not actually necessary to have it as a variable.

1 Like

Remembering that const will make the code uneditable, I think that let is just the same as just writing
rows * columns alone, so it’s really up to preferences if you want the code to be editable, use let or do nothing, and if you want the code to NOT be editable, use const.
Hopes this helps

1 Like