← Back to JavaScript Course | Chapter 12: Reference & Interview | Lesson 6 of 9

JS Mini Projects

Reading about individual JavaScript features one at a time is a bit like practicing scales on a piano, useful, but it's not until you play a real song that the pieces click together. These mini projects string multiple concepts, like event listeners, DOM updates, and state, into small, complete, working pieces of interactivity.

Click Counter

A click counter combines an event listener with a piece of state, a variable tracking the current count, and updates the visible text every time that state changes.

It's a compact demonstration of the core interactive pattern nearly every JavaScript app uses: react to an event, update state, reflect that state in the DOM.

उदाहरण: Click Counter

javascript
<button id="btn">Clicked 0 times</button>
<script>
  // Declare the variable `count`, set to `0`
  let count = 0;
  document.getElementById("btn").addEventListener("click", function () {
    count++;
    // Assign `Clicked ${count} times` to `this.textContent`
    this.textContent = `Clicked ${count} times`;
  });
</script>

To-Do List

A to-do list combines reading a value from a text input, creating a new DOM element for each item, and appending it to a list container, typically triggered by a button click or form submission.

It's a small but complete example of turning user input into persistent, visible page content.

उदाहरण: To-Do List

javascript
<input id="taskInput">
<button id="addBtn">Add</button>
<ul id="taskList"></ul>
<script>
  document.getElementById("addBtn").addEventListener("click", () => {
    // Declare the constant `li`, set to `document.createElement("li")`
    const li = document.createElement("li");
    // Assign `document.getElementById("taskInput").value` to `li.textContent`
    li.textContent = document.getElementById("taskInput").value;
    // Call `document.getElementById("taskList").appendChild(li)`
    document.getElementById("taskList").appendChild(li);
  });
</script>

Modal Popup

A modal popup is a hidden panel, usually styled to sit above the rest of the page, that toggles visible when triggered and hides again when dismissed, commonly by clicking outside of it or a close button.

The core logic is just adding or removing a class, or toggling the display style, in response to click events.

उदाहरण: Modal Popup

javascript
<button id="openBtn">Open Modal</button>
<div id="modal" style="display:none;">Modal content <button id="closeBtn">Close</button></div>
<script>
  // Show the modal when the Open button is clicked
  document.getElementById("openBtn").onclick = () => document.getElementById("modal").style.display = "block";
  // Hide the modal again when the Close button is clicked
  document.getElementById("closeBtn").onclick = () => document.getElementById("modal").style.display = "none";
</script>

Form Validation

Client-side form validation checks input values before allowing a submit to proceed, typically by preventing the default submit behavior and inspecting field values for emptiness or an expected pattern.

It gives users immediate feedback without waiting for a round trip to the server, though server-side validation is still required for real security.

उदाहरण: Form Validation

javascript
<form id="form">
  <input id="email">
  <button type="submit">Submit</button>
</form>
<script>
  document.getElementById("form").addEventListener("submit", (e) => {
    // Call `e.preventDefault()`
    e.preventDefault();
    // Check whether `!document.getElementById("email").value`
    if (!document.getElementById("email").value) {
      // Print "Email is required" to the console
      console.log("Email is required");
    }
  });
</script>

Combining Concepts: A Mini Quiz Widget

A small quiz widget ties together rendering data-driven content, listening for clicks on dynamically created options, comparing the selection against a known correct answer, and updating running state like a score.

It's a good exercise for practicing how several smaller JavaScript concepts combine into one interactive feature.

उदाहरण: Combining Concepts: A Mini Quiz Widget

javascript
<p id="question">2 + 2 = ?</p>
<button data-answer="4">4</button>
<button data-answer="5">5</button>
<p id="score">Score: 0</p>
<script>
  // Declare the variable `score`, set to `0`
  let score = 0;
  document.querySelectorAll("button").forEach(btn => {
    btn.addEventListener("click", () => {
      if (btn.dataset.answer === "4") score++;
      document.getElementById("score").textContent = "Score: " + score;
    });
  });
</script>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.