«Back to Home

Learn JavaScript

Topics

Parameters, Arguments, and Default Values

In the previous chapter, you learned the basics of functions—how to define and call them. But functions become much more powerful when you can send values into them. This is where parameters, arguments, and default values come in.

Understanding these concepts is essential for college students and recent graduates because almost every real-world program uses functions that accept inputs.

Let’s make this chapter very simple and clear.

What Are Parameters?

Parameters are placeholders inside a function.
They behave as empty containers that receive values when the function is called.

Example:

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

Here, name is a parameter.

What Are Arguments?

Arguments are the actual values you pass to the function when calling it.

Example:

greet("Riya");

Here, "Riya" is the argument.

So:

  • Parameter ? inside function

  • Argument ? when calling function

Example 1: Function With One Parameter

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

sayHello("Aman");
sayHello("Sita");

Output:

Hello Aman
Hello Sita

Example 2: Function With Two Parameters

function add(a, b) {
    console.log(a + b);
}

add(10, 20);
add(5, 7);

Output:

30
12

You can pass different arguments each time.

Example 3: Using Return With Parameters

function multiply(x, y) {
    return x * y;
}

let result = multiply(5, 4);
console.log(result);

Output:

20

The function returns a value that you can store or print.

Default Parameter Values

Sometimes you want a function to work even if no argument is provided.
In that case, you can set default values.

Syntax:

function functionName(parameter = defaultValue) {
    // code
}

Example:

function welcome(name = "Student") {
    console.log("Welcome " + name);
}

welcome("Rohan");
welcome(); // no argument

Output:

Welcome Rohan
Welcome Student

If no argument is passed, name becomes "Student" automatically.

Example: Function With Multiple Default Values

function bill(amount = 0, tax = 5) {
    console.log("Total:", amount + tax);
}

bill(100, 10);
bill(50);
bill();

Output:

Total: 110
Total: 55
Total: 5

This makes your function flexible and safe.

Real-Life Examples

1. Calculate the area of a rectangle

function area(length, width) {
    return length * width;
}

console.log(area(10, 5));

Output:

50

2. Print student details

function studentDetails(name, course) {
    console.log("Name:", name);
    console.log("Course:", course);
}

studentDetails("Meera", "BCA");

Output:

Name: Meera
Course: BCA

3. Greeting with default value

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

greetUser();
greetUser("Karan");

Output:

Hello Guest
Hello Karan

Common Mistakes Beginners Make

  1. Writing more arguments than parameters

  2. Forgetting to pass arguments while calling

  3. Mixing up argument order

  4. Confusing return with console.log

  5. Forgetting default values

Practice Tasks (Do It Yourself)

  1. Create a function that takes two numbers and prints their sum.

  2. Create a function that prints your name and city.

  3. Create a function that multiplies two numbers using return.

  4. Create a function with default values for name and age.

  5. Create a function to calculate the simple interest (P, R, T).

Author
Vijay Kumari
75 26k 949.3k