Codecademy Forums

Beat Mix: Why does one function use let where another use const?

Hey all,

So I was completely lost and decided to look at the solution to the Beat Mix Project (Project 2). I realize now it’s better to come to the forum and look for help but there was no Beat Mix posts and I had no specific questions to ask aside from “Where do I even begin??”.

Anyways, in the solution for presentHandler.js is the following code:

const presetHandler = (method, index, newPresetArray) => {
  if (method === 'GET') {
    let preset = getPreset(index);
    if (preset) {
      return [200, preset];
    } else {
      return [404];
    }
  } else if (method === 'PUT') {
    const newPreset = createOrUpdatePreset(index, newPresetArray);
    if (newPreset) {
      return [200, newPreset];
    } else {
      return [404];
    }
  } else {
    return [400];
  }
};

I’m confused why the GET method requires a let function whereas the PUT method uses const? They seem to be almost identical? Any help would be much appreciated. Thanks!

One line of thinking suggests that if the intention is to update a variable at some point, then use let, otherwise use const. In the code above preset and newPreset are never changed so const is fine to use. In such a simple function, it makes no difference which keyword we use. I tend to use let inside functions, and const to define global constants.

1 Like

mtf - I see you alot in these forums! Thank you so much for the reply! I will keep this in mind as I write and study more code.

1 Like
function ...
    const x ...
    for let i ...
    ...
    for let i ...
    ...

In this sense, x is a global to the two loop bodies. i is local only to the loop it exists in and not visible in that (or any) global scope.

1 Like