JavaScript Exercises

Practicing JavaScript with hands-on exercises is an excellent way to master the language and build problem-solving skills. Here are various types of exercises, ranging from basic to advanced, that cater to beginners, intermediates, and experienced developers.

Basic JavaScript Exercises

These exercises cover the fundamental concepts of JavaScript.

  1. Variables and Data Types
    • Create a variable to store your name and print it in the console.
    • Write a function that checks if a number is odd or even.
  2. Operators
    • Write a program to add, subtract, multiply, and divide two numbers.
    • Use the modulo operator to check if a number is divisible by 3.
  3. Conditional Statements
    • Write an if-else statement to find the largest of three numbers.
    • Use a switch statement to map numeric grades to letter grades.
  4. Loops
    • Write a loop to print the first 10 natural numbers.
    • Create a program that prints the multiplication table of a given number.

Intermediate JavaScript Exercises

These exercises focus on arrays, objects, and DOM manipulation.

  1. Arrays
    • Write a function that finds the largest number in an array.
    • Create an array of strings and sort them alphabetically.
  2. Objects
    • Create an object to represent a book with properties like title, author, and year. Write a function to display the book details.
    • Add a method to an object that calculates the age of the book based on the current year.
  3. Functions
    • Write a function that reverses a string.
    • Create a function that takes an array of numbers and returns their sum.
  4. DOM Manipulation
    • Write a script to change the text of a paragraph when a button is clicked.
    • Use JavaScript to dynamically create a list of items from an array and display it on a webpage.

Advanced JavaScript Exercises

These exercises cover ES6+ features, asynchronous programming, and more.

  1. ES6 and Beyond
    • Write a function using destructuring to extract and print specific properties from an object.
    • Use the spread operator to merge two arrays.
  2. Asynchronous JavaScript
    • Write a function using fetch() to retrieve and display data from a public API.
    • Create a program that uses Promise.all() to handle multiple promises.
  3. Error Handling
    • Write a function that handles and logs errors when parsing invalid JSON data.
  4. Higher-Order Functions
    • Use map() to double the numbers in an array.
    • Filter an array of objects to find people older than 30.

Example Exercise: Fibonacci Series

Problem: Write a function to generate the first n numbers in the Fibonacci series.

Solution:

function generateFibonacci(n) {
    let fib = [0, 1];
    for (let i = 2; i < n; i++) {
        fib.push(fib[i - 1] + fib[i - 2]);
    }
    return fib;
}
console.log(generateFibonacci(10));

Online JavaScript Exercise Platforms

Explore platforms offering interactive JavaScript exercises:

  • FreeCodeCamp: A comprehensive platform for JavaScript and web development.
  • HackerRank: Offers challenges to improve problem-solving and coding skills.
  • Codewars: Focuses on competitive coding exercises.
  • The Coding College: Visit for exclusive JavaScript tutorials and practice resources.

Consistently practicing these exercises will help solidify your JavaScript knowledge and prepare you for real-world coding challenges!

Leave a Comment