Skip to content

How to call a function in JavaScript

  • by

To call a function in JavaScript, use the function name followed by parentheses. Functions are used to execute a block of code that performs a specific task or returns a value, making code more organized, easier to read, and more maintainable.

Use the function name followed by parentheses, like this:

function myFunction() {
  // code to be executed
}

// Call the function
myFunction();

When you call the function using myFunction(), the code inside the function will be executed.

If the function takes parameters, you can pass them inside the parentheses when you call the function.

function greet(name) {
  console.log(`Hello, ${name}!`);
}

greet("John"); // Output: Hello, John!

You can call functions in JavaScript whenever you need to perform a task, handle an event, return a value, or make your code more modular.

How to call a function in JavaScript example

Simple example code of how to call a function in JavaScript:

<!DOCTYPE html>
<html>
<head>
    <script>
    // Define the function
    function calculateTotalPrice(price, quantity) {
        const totalPrice = price * quantity;
    return totalPrice;
    }

    // Call the function and store the result in a variable
    const price = 10;
    const quantity = 5;
    const total = calculateTotalPrice(price, quantity);

    // Output the result to the console
    console.log(`The total price is ${total}.`);

    </script>
</head>
<body>

</body>
</html>

Output:

How to call a function in JavaScript

Another Practical example


function checkAge(age) {
  if (age >= 18) {
    console.log("You are old enough to vote.");
  } else {
    console.log("You are not old enough to vote yet.");
  }
}


checkAge(22); //You are old enough to vote.
checkAge(16); //You are not old enough to vote yet.
checkAge(18); //You are old enough to vote.

When Should You Call a Function in JavaScript?

Answer: You should call a function in JavaScript whenever you want to execute a block of code that performs a specific task or returns a value.

Here are some situations where you might want to call a function in JavaScript:

SituationExplanation
Perform a specific taskIf you have a task that needs to be performed repeatedly or that requires multiple lines of code to be executed, you can create a function to encapsulate that logic and call the function whenever you need to perform that task.
Handle an eventJavaScript functions are often used to handle events, such as user clicks, mouse movements, or keyboard inputs. When an event is triggered, the corresponding function is called, allowing you to execute the desired behavior in response to the event.
Return a valueFunctions can also be used to return a value, which can be used elsewhere in your code. For example, you might create a function that calculates the average of a set of numbers, and then call that function whenever you need to calculate the average of a new set of numbers.
Modular codeBy breaking your code into smaller functions, you can make your code more modular and easier to maintain. This allows you to focus on individual parts of your code without having to worry about the entire program at once.

Comment if you have any doubts or suggestions on this JS function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *