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: 
Even `i` is in global scope after the loop value of `i` would be array length so it would be like `array[array.length]` which will be `undefined`. To make it work append the value of `i` at that point along with string.

```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);
}
```

---

Or in case the array is not in global scope then you have to append the value itself as a string argument(wrap with quotes).

```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);
}
```

---

***FYI :*** `document.body.innerHTML +=...` is a really bad idea since it will always recreate DOM elements which will remove any attached event handler, properties,etc... So always keep a variable within the loop to keep the HTML string and then update content finally(in your case).
Maybe what you need is string interpolation

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

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

}

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

```

This will pass the value of `allblocks[i]` instead of just a string