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
functionis used to create a function.greetis the function name.- The code inside
{}is the function body. console.log()displays the message.greet()calls the function.- 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
nameis a parameter of the function.- When
greetUser("Rahul")is called,"Rahul"is passed toname. - The function combines
"Hello, "with the name. - 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
- The function
addNumbers()accepts two parameters:aandb. 10is passed toa.20is passed tob.- The function adds
a + b. 10 + 20gives30.- 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
- The function accepts
aandb. returnsends a value back from the function.15 + 25produces40.- The returned value is stored in
result. console.log(result)displays40.
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
checkEvenOdd()accepts a number.- The
%operator finds the remainder. - If the remainder after division by
2is0, the number is even. - Otherwise, the number is odd.
12 % 2is0.- 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
- The
square()function accepts one parameter. - The number is multiplied by itself.
6 × 6equals36.returnsends36back to the calling code.- The returned value is stored in
result. - 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
- The function has two parameters:
nameandmarks. "Priya"is passed as the first argument.85is passed as the second argument.- The function displays both values.
- 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
name = "Guest"creates a default parameter.- When the function is called without an argument,
"Guest"is used. greetUser()therefore displays"Welcome, Guest".- When
"Aman"is passed, the provided value is used instead. - 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
calculateTotal()acceptspriceandquantity.- The price is
500. - The quantity is
3. - The function multiplies both values.
500 × 3equals1500.- The function returns
1500. - 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
- A function expression stores a function inside a variable.
multiplystores the function.- The function accepts
aandb. a * bcalculates the multiplication.multiply(7, 8)calls the function.7 × 8gives56.- 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
functionkeyword. - A function runs when it is called.
- Parameters allow functions to receive data.
- Arguments are the actual values passed to parameters.
returnsends 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.
