Constant used within repeat loop

Asked

Viewed 106 times

3

const body = document.querySelector("body");
const divElement = document.createElement("div");

divElement.setAttribute("id", "resultado");
body.appendChild(divElement);

for(i = 1; i <= 20; i++) {
    const textElement = document.createElement("p");
    textElement.innerHTML = "Resultado: " + i;
    divElement.append(textElement);
}

What happens exactly inside the for with the const textElement? (The code works normally, but I was wondering what’s going on over there with that constant)

  • A array with 20 positions (textElement[1], textElement[2]... textElement[n])?
  • To constante is superscripted?
  • A new value is obtained for each loop?
  • If it is none of the above causes, what happens then?

References: Alura, Wiki

  • The constant is within the scope of the for, that is, there will only be the constant in the current iteration and in the next cycle the constant will be declared again, pq ai will be another iteration scope.

1 answer

3


Create an array with 20 positions (textElement1, textElement2... textElement[n])?

No, you must be confusing what this object is. It is a manipulator of the GIFT, even is the GIFT, so it has a form to refer to an element of the GIFT, only this, has not array or any other value of your code. We can say that it is a kind of proxy to the gift.

The constant is superscripted?

In each execution a new constant object is created, the constant is the same. Read this to understand it better.

A new value is obtained for each loop?

Yes, of course it may be the same value, but a new object.

If it is none of the above causes, what happens then?

The problem of how to manipulate the DOM is engine of Javascript. At the time you use innerHTML something will happen in the DOM, but it’s not your problem. When he picks up a new object he will present the state of the GIFT at the time including what has already been changed. The GIFT is obviously mutable. You do not have access to it directly, only through these manipulative objects, it is implementation detail.

  • So this loop has about 20 objects? If not an object, but only a value like const a = 10, would be the same answer to the question (in this case create 20 constants with 20 equal values, in case 10?)

  • 1

    No, there is only one GIFT, it creates 20 object to manipulate the GIFT. But each of them will be discarded at the end of the loop step. If it were anything else, the result would be something else. As amazing as it sounds, it looks a lot like this: https://answall.com/a/419022/101. What you’re doing is manipulating the object you have no control over in your algorithm. If you go to do it in a local variable everything changes, and then you have to store it in a variable. I will try to find a question that shows which variable created within the loop only exists while the step is running, in another step is another Let/const

Browser other questions tagged

You are not signed in. Login or sign up in order to post.