Question: I do not understand why after clicking the link "undefined" is shown. Does anyone know why?

```js
let allBlocks = ["one","two"];

for (i = 0; i < allBlocks.length; i++) {
  document.body.innerHTML += '<p id="" onclick="test(allBlocks[i])">Link</p>';
}

function test(n){
    alert(n);
}

```

Answer: 
Because your code in the click handler is

```
test(allBlocks[i])

```

It would appear your code is at global scope, which is the only reason it's not throwing an error. When the click occurs, `i` has the value `allBlocks.length`, which is beyond the end of the array. Accessing an array entry that isn't there results in `undefined`.

The minimum change is to use string concatenation to put the value of `i` in the click handler rather than `i`:

```
document.body.innerHTML += '<p id="" onclick="test(allBlocks[' + i + '])">Link</p>';

```

However, rather than doing that, I'd suggest modern event handling via `addEventListener`:

```
for (let i = 0; i < allBlocks.length; i++) {
//   ^^^---- Note
  const p = document.createElement("p");
  p.textContent = "Link";
  p.addEventListener("click", test.bind(null, allBlocks[i]));
  document.body.appendChild(p);
}

```

---

Side note: `element.innerHTML += ...` is never a good idea. It forces the browser to loop through the entire contents of `element`, building up an HTML string for the DOM structure it contains, and then pass that string to the JavaScript layer; then the JavaScript layer has to add to the string and pass it back to the browser; then the browser has to parse the HTML, creating a bunch of new, replacement elements, wipe out the contents of `element`, and replace it with those new elements. Other than being a lot of unnecessary work, it also destroys any event handlers on the elements, any state information, etc.
..because `allBlocks[i]` is undefined. You need the value of `i` to be rendered in your HTML. 

```js
let allBlocks = ["one","two"];

for (i = 0; i < allBlocks.length; i++) {

  document.body.innerHTML += '<p id="" onclick="test(allBlocks['+i+'])">Link</p>';

}

function test(n){
    alert(n);
}
```