JavaScript Functions Practice Questions with Solutions

Intriductions

Functions are one of the most important concepts in JavaScript. A function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, you can put it inside a function and call it whenever needed. In this chapter, you will practice creating functions, calling functions, using parameters, returning values, and working with default parameters through simple examples. JavaScript functions practice questions with solutions help to understand the concepts.

Question 1: Create and Call a Function

Problem

Create a function named greet that displays "Hello, JavaScript!" when it is called.

Solution

function greet() {
    console.log("Hello, JavaScript!");
}

greet();

Output

Hello, JavaScript!

Step-by-step Explanation

  1. function is used to create a function.
  2. greet is the function name.
  3. The code inside {} is the function body.
  4. console.log() displays the message.
  5. greet() calls the function.
  6. The function runs when it is called.

Question 2: Create a Function with a Parameter

Problem

Create a function named greetUser that accepts a person’s name and displays a greeting.

Solution

function greetUser(name) {
    console.log("Hello, " + name);
}

greetUser("Rahul");

Output

Hello, Rahul

Step-by-step Explanation

  1. name is a parameter of the function.
  2. When greetUser("Rahul") is called, "Rahul" is passed to name.
  3. The function combines "Hello, " with the name.
  4. The final message is displayed.

Here:

greetUser("Rahul");

"Rahul" is called an argument.


Question 3: Add Two Numbers Using a Function

Problem

Create a function that accepts two numbers and displays their sum.

Solution

function addNumbers(a, b) {
    let sum = a + b;

    console.log(sum);
}

addNumbers(10, 20);

Output

30

Step-by-step Explanation

  1. The function addNumbers() accepts two parameters: a and b.
  2. 10 is passed to a.
  3. 20 is passed to b.
  4. The function adds a + b.
  5. 10 + 20 gives 30.
  6. The result is displayed.

Question 4: Return a Value from a Function

Problem

Create a function that accepts two numbers and returns their sum.

Solution

function addNumbers(a, b) {
    return a + b;
}

let result = addNumbers(15, 25);

console.log(result);

Output

40

Step-by-step Explanation

  1. The function accepts a and b.
  2. return sends a value back from the function.
  3. 15 + 25 produces 40.
  4. The returned value is stored in result.
  5. console.log(result) displays 40.

The important difference is:

console.log(a + b);

displays a value, while:

return a + b;

sends the value back so it can be used elsewhere.


Question 5: Create a Function to Check Even or Odd

Problem

Create a function that accepts a number and displays whether it is even or odd.

Solution

function checkEvenOdd(number) {
    if (number % 2 === 0) {
        console.log("Even");
    } else {
        console.log("Odd");
    }
}

checkEvenOdd(12);

Output

Even

Step-by-step Explanation

  1. checkEvenOdd() accepts a number.
  2. The % operator finds the remainder.
  3. If the remainder after division by 2 is 0, the number is even.
  4. Otherwise, the number is odd.
  5. 12 % 2 is 0.
  6. Therefore, "Even" is displayed.

Question 6: Create a Function to Calculate Square

Problem

Create a function that accepts a number and returns its square.

Solution

function square(number) {
    return number * number;
}

let result = square(6);

console.log(result);

Output

36

Step-by-step Explanation

  1. The square() function accepts one parameter.
  2. The number is multiplied by itself.
  3. 6 × 6 equals 36.
  4. return sends 36 back to the calling code.
  5. The returned value is stored in result.
  6. The result is displayed.

Question 7: Use Multiple Parameters

Problem

Create a function that accepts a student’s name and marks, then displays both values.

Solution

function showStudent(name, marks) {
    console.log("Name: " + name);
    console.log("Marks: " + marks);
}

showStudent("Priya", 85);

Output

Name: Priya
Marks: 85

Step-by-step Explanation

  1. The function has two parameters: name and marks.
  2. "Priya" is passed as the first argument.
  3. 85 is passed as the second argument.
  4. The function displays both values.
  5. Multiple parameters allow a function to work with multiple pieces of information.

Question 8: Use a Default Parameter

Problem

Create a function that greets a user. If no name is provided, it should use "Guest" as the default name.

Solution

function greetUser(name = "Guest") {
    console.log("Welcome, " + name);
}

greetUser();
greetUser("Aman");

Output

Welcome, Guest
Welcome, Aman

Step-by-step Explanation

  1. name = "Guest" creates a default parameter.
  2. When the function is called without an argument, "Guest" is used.
  3. greetUser() therefore displays "Welcome, Guest".
  4. When "Aman" is passed, the provided value is used instead.
  5. The second call displays "Welcome, Aman".

Question 9: Calculate Total Price Using a Function

Problem

Create a function that accepts a product price and quantity and returns the total price.

Solution

function calculateTotal(price, quantity) {
    return price * quantity;
}

let total = calculateTotal(500, 3);

console.log(total);

Output

1500

Step-by-step Explanation

  1. calculateTotal() accepts price and quantity.
  2. The price is 500.
  3. The quantity is 3.
  4. The function multiplies both values.
  5. 500 × 3 equals 1500.
  6. The function returns 1500.
  7. The returned value is stored in total.

Question 10: Create a Function Expression

Problem

Create a function expression named multiply that accepts two numbers and returns their multiplication.

Solution

const multiply = function(a, b) {
    return a * b;
};

let result = multiply(7, 8);

console.log(result);

Output

56

Step-by-step Explanation

  1. A function expression stores a function inside a variable.
  2. multiply stores the function.
  3. The function accepts a and b.
  4. a * b calculates the multiplication.
  5. multiply(7, 8) calls the function.
  6. 7 × 8 gives 56.
  7. The returned value is stored in result.

A function expression is different from a traditional function declaration:

function multiply(a, b) {
    return a * b;
}

Both can be used to create reusable functions.

Key Takeaways

  • Functions are reusable blocks of code.
  • Functions are created using the function keyword.
  • A function runs when it is called.
  • Parameters allow functions to receive data.
  • Arguments are the actual values passed to parameters.
  • return sends a value back from a function.
  • A function can have multiple parameters.
  • Default parameters provide fallback values.
  • Functions can perform calculations and return results.
  • A function expression stores a function inside a variable.
  • Reusable functions help avoid repeating the same code.

FAQs

1. What is a function in JavaScript?

A function is a reusable block of code designed to perform a specific task.

function greet() {
    console.log("Hello");
}

greet();

2. Why are functions used in JavaScript?

Functions help organize code, reduce repetition, and make programs easier to maintain and reuse.

3. What is a parameter?

A parameter is a variable listed inside a function’s parentheses.

function greet(name) {
    console.log(name);
}

Here, name is a parameter.

4. What is an argument?

An argument is the actual value passed to a function when it is called.

greet("Rahul");

Here, "Rahul" is an argument.

5. What does return do in a function?

return sends a value from the function back to the code that called it.

function add(a, b) {
    return a + b;
}

let result = add(10, 20);

The value 30 is returned and stored in result.

6. What is a default parameter?

A default parameter provides a value when an argument is not supplied.

function greet(name = "Guest") {
    console.log(name);
}

If you call greet(), the function uses "Guest".

7. What is a function expression?

A function expression is a function stored in a variable.

const add = function(a, b) {
    return a + b;
};

The function can then be called using:

add(10, 20);

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top