Skip to main content
100%

How good your JS?

✓ Published0🌍 Public
TTipTopCoder
Last edited Jan 14, 2020
Created on Jan 14, 2020

This example demonstrates a common JavaScript closure pitfall inside a loop. The code defines an `addLinks` function that creates five anchor elements, each intended to alert its index when clicked. However, because the loop variable `i` is captured by reference in the click handler, every link alerts the final value, 5, instead of its own index. The visualization relies on the browser’s DOM API, including `document.createElement` and `element.onclick`, with the result shown when `window.onload` triggers the function. It highlights the difference between expected and actual behavior in event handling.

AI-generated description

Similar vizzes