Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
A reusable initializer receives a root element and finds controls inside it. State declared inside that initializer belongs to one instance. Global selectors or a single shared counter can accidentally connect independent widgets. Build a counter twice using the same function, then verify that interacting with one leaves the other unchanged. A component can own its own nodes and state. Two instances should not accidentally update each other.
function mountCounter(root){let value=0;root.querySelector('button').addEventListener('click',()=>{root.querySelector('output').textContent=String(++value);});}
document.querySelectorAll('.counter').forEach(mountCounter);Run the preview tests; inspect the live page.
A reusable initializer receives a root element and finds controls inside it. State declared inside that initializer belongs to one instance. Global selectors or a single shared counter can accidentally connect independent widgets. Build a counter twice using the same function, then verify that interacting with one leaves the other unchanged.
function mountCounter(root){let value=0;root.querySelector('button').addEventListener('click',()=>{root.querySelector('output').textContent=String(++value);});}Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.
document.querySelectorAll('.counter').forEach(mountCounter);Execute this statement at this point in the program. Follow its arguments and returned value using the surrounding concept explanation.
function mountCounter(root){let value=0;root.querySelector('button').addEventListener('click',()=>{root.querySelector('output').textContent=String(++value);});}
document.querySelectorAll('.counter').forEach(mountCounter);
console.log(document.querySelectorAll("*").length);Interact with the preview; the diagnostic displays its actual element count.
A reusable initializer receives a root element and finds controls inside it. State declared inside that initializer belongs to one instance. Global selectors or a single shared counter can accidentally connect independent widgets. Build a counter twice using the same function, then verify that interacting with one leaves the other unchanged. A component can own its own nodes and state. Two instances should not accidentally update each other. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
function mountCounter(root){let value=0;root.querySelector('button').addEventListener('click',()=>{root.querySelector('output').textContent=String(++value);});}Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.
document.querySelectorAll('.counter').forEach(mountCounter);Execute this statement at this point in the program. Follow its arguments and returned value using the surrounding concept explanation.
console.log(document.querySelectorAll("*").length);Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function mountCounter(root){let value=0;root.querySelector('button').addEventListener('click',()=>{root.querySelector('output').textContent=String(++value);});}
document.querySelectorAll('.counter').forEach(mountCounter);
console.log(document.querySelectorAll("button").length);Interact with the preview; the diagnostic displays its actual element count.
A reusable initializer receives a root element and finds controls inside it. State declared inside that initializer belongs to one instance. Global selectors or a single shared counter can accidentally connect independent widgets. Build a counter twice using the same function, then verify that interacting with one leaves the other unchanged. A component can own its own nodes and state. Two instances should not accidentally update each other. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
function mountCounter(root){let value=0;root.querySelector('button').addEventListener('click',()=>{root.querySelector('output').textContent=String(++value);});}Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.
document.querySelectorAll('.counter').forEach(mountCounter);Execute this statement at this point in the program. Follow its arguments and returned value using the surrounding concept explanation.
console.log(document.querySelectorAll("button").length);Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
Initialize each .counter with independent state starting at zero; its button adds one.
// Initialize each .counter with independent state starting at zero; its button adds one. // Use the starting HTML shown below the mission.
function mountCounter(root){let value=0;root.querySelector('button').addEventListener('click',()=>{root.querySelector('output').textContent=String(++value);});}
document.querySelectorAll('.counter').forEach(mountCounter);Accept a root parameter and select beneath that root.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.