JavaScript practical exercises

Below is a set of JavaScript practical exercises designed to cover the full course content, progressing from simple to advanced concepts. These exercises align with the syllabus and focus on teaching JavaScript programming skills for web development. Each exercise includes a description, syllabus coverage (where applicable), aim, and JavaScript code.


Exercise 1: Basic JavaScript Syntax

Description : Write a JavaScript program that displays “Hello, World!” in the browser console and on a webpage.
Syllabus Coverage : Introduction to JavaScript, embedding scripts into web pages.
Aim : Introduce students to basic JavaScript syntax and its integration with HTML.

Exercise 2: Variables and Data Types

Description : Write a JavaScript program that declares variables for a student’s name, age, and grade, and displays them on a webpage.
Syllabus Coverage : Primitive data types, variables.
Aim : Teach students how to declare and use variables in JavaScript.

Exercise 3: Arrays

Description : Write a JavaScript program that creates an array of student names and displays them in a list format on a webpage.
Syllabus Coverage : Arrays.
Aim : Introduce students to arrays and their usage in JavaScript.

Exercise 4: Control Structures

Description : Write a JavaScript program that checks if a number is even or odd and displays the result on a webpage.
Syllabus Coverage : Control structures (selection).
Aim : Teach students how to use conditional statements in JavaScript.

Exercise 5: Loops

Description : Write a JavaScript program that prints numbers from 1 to 10 using a loop and displays them on a webpage.
Syllabus Coverage : Control structures (repetition).
Aim : Teach students how to use loops in JavaScript.

Exercise 6: Functions

Description : Write a JavaScript program that defines a function to calculate the area of a rectangle and calls it with user-provided values.
Syllabus Coverage : Sub-programs (functions), parameter passing, return values.
Aim : Introduce students to writing reusable functions in JavaScript.

Exercise 7: Event Handling

Description : Write a JavaScript program that changes the background color of a webpage when a button is clicked.
Syllabus Coverage : Event handling.
Aim : Teach students how to handle user interactions using JavaScript.

Exercise 8: DOM Manipulation

Description : Write a JavaScript program that dynamically adds a new paragraph to a webpage when a button is clicked.
Syllabus Coverage : DOM manipulation.
Aim : Teach students how to manipulate the Document Object Model (DOM).

Exercise 9: Forms and User Input

Description : Write a JavaScript program that collects user input from a form and displays it on the webpage.
Syllabus Coverage : Forms, input elements, GET/POST methods.
Aim : Teach students how to handle user input using forms in JavaScript.

Exercise 10: Fetch API

Description : Write a JavaScript program that fetches data from a public API (e.g., JSONPlaceholder) and displays it on a webpage.
Syllabus Coverage : Advanced JavaScript concepts (API integration).
Aim : Introduce students to working with APIs and asynchronous programming.

Exercise 11: Local Storage

Description : Write a JavaScript program that saves user preferences (e.g., theme color) to local storage and retrieves them when the page reloads.
Syllabus Coverage : Advanced JavaScript concepts (local storage).
Aim : Teach students how to persist data using local storage.

Exercise 12: Asynchronous Programming with Promises

Description : Write a JavaScript program that simulates an asynchronous task using Promises and displays the result.
Syllabus Coverage : Advanced JavaScript concepts (Promises).
Aim : Introduce students to asynchronous programming using Promises.

Exercise 13: Classes and Objects

Description : Write a JavaScript program that defines a class for a “Student” and creates objects to display student details.
Syllabus Coverage : Object-oriented programming (OOP).
Aim : Teach students how to use classes and objects in JavaScript.

Above exercises comprehensively cover JavaScript-related topics, including variables , arrays , control structures , functions , event handling , DOM manipulation , forms , API integration , local storage , asynchronous programming , and OOP . They are designed to progressively build students’ skills while aligning with the learning outcomes.

Below are advanced JavaScript exercises that go beyond the syllabus, exploring modern and cutting-edge concepts in web development. These exercises will help you deepen your understanding of JavaScript and its capabilities in building dynamic, interactive, and scalable web applications.


Exercise 1: Asynchronous Programming with async/await

Description : Write a JavaScript program that uses async/await to fetch data from an API and handle errors gracefully.
What You Learn : Modern asynchronous programming techniques using async/await.


Exercise 2: Modular JavaScript with ES6 Modules

Description : Create a modular JavaScript application using ES6 import and export.
What You Learn : Modularizing code for better organization and reusability.

File: math.js

File: app.js


Exercise 3: Building a Custom JavaScript Library

Description : Create a small JavaScript library that provides utility functions like DOM manipulation, event handling, and AJAX requests.
What You Learn : Writing reusable libraries and encapsulating functionality.


Exercise 4: Implementing a Publish-Subscribe Pattern

Description : Build a simple event bus using the publish-subscribe pattern to manage custom events.
What You Learn : Event-driven architecture and decoupling components.


Exercise 5: Creating a Single Page Application (SPA) with JavaScript

Description : Build a simple SPA using JavaScript to handle routing without a framework.
What You Learn : Client-side routing and dynamic content loading.


Exercise 6: Working with Web Workers

Description : Use Web Workers to perform heavy computations without blocking the main thread.
What You Learn : Offloading tasks to background threads for better performance.

File: worker.js

File: main.js

Exercise 7: Building a Real-Time Chat Application with WebSocket

Description : Create a real-time chat application using WebSocket for instant communication.
What You Learn : Real-time communication and WebSocket protocol.

Exercise 8: Using Proxies for Object Observation

Description : Use JavaScript Proxies to observe changes to an object’s properties.
What You Learn : Advanced object manipulation and reactive programming concepts.

Exercise 9: Implementing a Virtual DOM

Description : Build a simplified version of a virtual DOM to understand how frameworks like React work under the hood.
What You Learn : Efficient DOM updates and diffing algorithms.

Exercise 10: Using Generators for Iteration

Description : Write a JavaScript program that uses generators to create an infinite sequence of numbers.
What You Learn : Lazy evaluation and generator functions.

Exercise 11: Building a Custom Promise Implementation

Description : Implement a simplified version of JavaScript Promises from scratch.
What You Learn : Understanding Promises and their internal mechanics.

Exercise 12: Using Intersection Observer for Lazy Loading

Description : Implement lazy loading of images using the Intersection Observer API.
What You Learn : Efficient resource loading and performance optimization.

These exercises explore advanced JavaScript concepts such as asynchronous programming , modularization , custom libraries , publish-subscribe patterns , SPAs , Web Workers , Proxies , virtual DOM , generators , custom promises , and lazy loading . They provide practical, real-world applications of JavaScript and will help you explore areas beyond the syllabus.

Html Editor

External Editor (Html. CSS, Java Scripts)

Spread the love