Understanding when const is used e.g. inside

I not understanding the time consts can be used even though the variable value is different across the program. I know it at least can have to do with the scope, it is defined but can get confused beyond that. From looking at a program it appears a scope, is a particular iteration of a loop. For example in below example (taken from Learn Basic Algorithmic Thinking by Building a Number Sorter: Step 45 | freeCodeCamp.org the program I was refering to) it is how come the const key word is used instead of let for temp. I am wrong or partially wrong.

const bubbleSort = (array) => {
  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array.length - 1; j++) {
      if (array[j] > array[j + 1]) {
        const temp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = temp;
      }
    }
  }

  return array;
}

temp exists in that scope only, and does not need to be reassigned or changed

you can start declaring all variables with const, unless you need to declare them without a value, and change them to let if you find out that with const it disallow a thing you need to do

What is temp’s is scope. What I said was not with confidence.

temp exists only in the block {} it is declared, in fact if you do this:

you get an error as you would be trying to access it out of scope

1 Like

The general advice for when to use const is that you should use const if you can - as long as you don’t need to reassign the variable, use const.

1 Like

From a test I did I take for a loop the scope is all iterations of the loop. Please let me know if iteration is not a widely understood/ or even correct term and an alternative. Let me know if you don’t know what I mean by iteration and I will explain.

The scope cannot be across all iterations of the loop - each loop iteration has a different value for temp. Therefore, each loop iteration has it’s own scope.

I think I may have not explained myself well here. I could well have but want to be sure. To be clear I was refering to a circumstance like my test variable in the code below.

const bubbleSort = (array) => {
  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array.length - 1; j++) {
      if(j>=1){
        console.log(test)
      }
      if (array[j] > array[j + 1]) {
        const temp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = temp;
      }
      const test=i+j;
    }
  }

Your variable test is scoped to individual iterations of the i loop.

1 Like

I take from your reply, iteration is a widely understood and suitable term.

Iteration is a common term. I’m not sure if you and I mean the same thing by it though.

1 Like

Thankyou that makes it clear it was not how I explained it.

Thankyou for all your help, things are cleared up. One last point is const keyword named after constant or something else.

According to MDN, it looks like it.

Right on off chance it was something easier it make have made thing easier. I just focus on this is what the javaScript const key word means and how to use it. If what to consider it not aligning to how you consider, constants you can.

I consider this is also a solution. With it own advantages over the one have ticked as so.