Excited to share a collection of JavaScript exercises centered around Functions

๐Ÿš€ Excited to share a collection of JavaScript exercises centered around Functions! ๐Ÿš€

Diving deep into JavaScript functions enhances not only your understanding but also your problem-solving and coding efficiency. From the basics of declaring functions, to more complex concepts like closures and recursion, mastering functions is a critical skill in any developer’s toolkit.

In this set:

– Explore different types of functions: Regular, Arrow, Anonymous, and more.

– Learn how to utilize parameters effectively, including default and rest parameters.

– Understand the significance of scope, callbacks, and recursive functions in practical scenarios.

Why tackle this? Functions form the backbone of JavaScript programming, enabling modular, readable, and reusable code. They are essential for event handling, callback manipulation, and maintaining clean code architecture.

Check out the exercises, and let’s start a conversation on your approaches, insights, or any cool function tricks you’ve discovered in your coding journey! 

Share your thoughts or solutions below! Let’s elevate our collective knowledge and make JavaScript a little less daunting for everyone. #JavaScript #WebDevelopment #CodingExercises #LearnToCode

JavaScript Functions

Exercise 1: Declaring a Function

Problem: Write a function named greet that prints “Hello, World!” to the console.

Explanation: This exercise introduces the basic syntax for declaring a function in JavaScript. Functions are reusable blocks of code that perform a specific task.


function greet() {

 console.log(“Hello, World!”);


greet(); // Call the function

Exercise 2: Function with Parameters

Problem: Create a function sum that takes two parameters, a and b, and logs their sum.

Explanation: Functions can take parameters, allowing you to pass values into them. This exercise demonstrates defining and calling a function with parameters.


function sum(a, b) {

 console.log(a + b);


sum(5, 7); // Outputs: 12

Exercise 3: Return Value

Problem: Write a function multiply that returns the product of two numbers.

Explanation: Functions can return values using the return statement. This value can then be used where the function is called.


function multiply(a, b) {

 return a * b;


console.log(multiply(6, 7)); // Outputs: 42

Exercise 4: Default Parameters

Problem: Create a function greet that takes a name as a parameter and greets the person. If no name is provided, it should greet “Guest”.

Explanation: Default parameters allow you to initialize function parameters with default values if no value or undefined is passed.


function greet(name = “Guest”) {

 console.log(“Hello, ” + name + “!”);


greet(“Alice”); // Outputs: Hello, Alice!

greet(); // Outputs: Hello, Guest!

Exercise 5: Arrow Functions

Problem: Convert a function expression add into an arrow function.

Explanation: Arrow functions provide a shorter syntax for writing function expressions. They are anonymous and change the way this binds in functions.


const add = (a, b) => a + b;

console.log(add(10, 5)); // Outputs: 15

Exercise 6: Anonymous Functions

Problem: Create an anonymous function that logs “I am anonymous!” and immediately invoke it.

Explanation: Anonymous functions are functions without a name. They can be used as IIFE (Immediately Invoked Function Expression) to execute code immediately.


(function() {

 console.log(“I am anonymous!”);


Exercise 7: Callback Functions

Problem: Write a function processUserInput that takes a callback function as a parameter. The processUserInput function should call the callback function with a sample user input.

Explanation: Callback functions are passed to other functions as arguments and can be called within the outer function to complete some kind of routine or action.


function processUserInput(callback) {

 const name = “Alice”;



processUserInput(name => {

 console.log(“Hello, ” + name + “!”);


Exercise 8: Function Scope

Problem: Create two variables in different scopes but with the same name, and log their values inside and outside a function.

Explanation: This exercise demonstrates how local and global scope work in JavaScript. Variables defined inside a function are not accessible from outside the function.


let animal = ‘Lion’; // Global scope

function showAnimal() {

 let animal = ‘Tiger’; // Local scope

 console.log(animal); // Tiger



console.log(animal); // Lion

Exercise 9: Rest Parameters

Problem: Write a function sumAll that uses rest parameters to take any number of arguments and returns their sum.

Explanation: Rest parameters allow a function to accept an indefinite number of arguments as an array.


function sumAll(…numbers) {

 return numbers.reduce((acc, current) => acc + current, 0);


console.log(sumAll(1, 2, 3, 4)); // Outputs: 10

Exercise 10: Recursive Function

Problem: Write a recursive function factorial that calculates the factorial of a number.

Explanation: A recursive function is a function that calls itself until it doesnโ€™t. The factorial of a number is the product of all positive integers less than or equal to the number.


function factorial(n) {

 if (n === 0) {

 return 1;

 } else {

 return n * factorial(n – 1);



console.log(factorial(5)); // Outputs: 120