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 UI often displays a value stored in JavaScript. Keep that value in one variable, update it after an action and render it into the DOM. Reading a string from textContent and doing arithmetic can cause coercion mistakes. Separating state from presentation makes reset, limits and testing clearer. This counter must handle multiple clicks, not only one. Application state should be explicit. The current count belongs in one place, while the interface displays it.
let count = 0;
document.querySelector('#increment').addEventListener('click', () => { count += 1; document.querySelector('#count').textContent = count; });Run the preview tests; inspect the live page.
A UI often displays a value stored in JavaScript. Keep that value in one variable, update it after an action and render it into the DOM. Reading a string from textContent and doing arithmetic can cause coercion mistakes. Separating state from presentation makes reset, limits and testing clearer. This counter must handle multiple clicks, not only one.
let count = 0;Declare count with let. The binding may be reassigned later. The expression after = supplies its initial value; = is assignment, not an equality comparison.
document.querySelector('#increment').addEventListener('click', () => { count += 1; document.querySelector('#count').textContent = count; });Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
let count = 0;
document.querySelector('#increment').addEventListener('click', () => { count += 1; document.querySelector('#count').textContent = count; });
console.log(document.querySelectorAll("*").length);Interact with the preview; the diagnostic displays its actual element count.
A UI often displays a value stored in JavaScript. Keep that value in one variable, update it after an action and render it into the DOM. Reading a string from textContent and doing arithmetic can cause coercion mistakes. Separating state from presentation makes reset, limits and testing clearer. This counter must handle multiple clicks, not only one. Application state should be explicit. The current count belongs in one place, while the interface displays it. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
let count = 0;Declare count with let. The binding may be reassigned later. The expression after = supplies its initial value; = is assignment, not an equality comparison.
document.querySelector('#increment').addEventListener('click', () => { count += 1; document.querySelector('#count').textContent = count; });Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
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.
let count = 0;
document.querySelector('#increment').addEventListener('click', () => { count += 1; document.querySelector('#count').textContent = count; });
console.log(document.querySelectorAll("button").length);Interact with the preview; the diagnostic displays its actual element count.
A UI often displays a value stored in JavaScript. Keep that value in one variable, update it after an action and render it into the DOM. Reading a string from textContent and doing arithmetic can cause coercion mistakes. Separating state from presentation makes reset, limits and testing clearer. This counter must handle multiple clicks, not only one. Application state should be explicit. The current count belongs in one place, while the interface displays it. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
let count = 0;Declare count with let. The binding may be reassigned later. The expression after = supplies its initial value; = is assignment, not an equality comparison.
document.querySelector('#increment').addEventListener('click', () => { count += 1; document.querySelector('#count').textContent = count; });Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
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.
Increment #count on every #increment click.
// Increment #count on every #increment click. // Write your JavaScript below.
let count = 0;
document.querySelector('#increment').addEventListener('click', () => { count += 1; document.querySelector('#count').textContent = count; });Declare count outside the listener so it survives between clicks.
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.