How to call JavaScript function in an alert box?


To call a JavaScript function in an alert box, you can use the alert() function, a built-in function in JavaScript that displays an alert dialog with a specified message.

An alert box is a simple dialog box that displays a message to the user and provides an OK button. It is a built-in function in JavaScript that is often used for debugging or displaying simple notifications to the user.

Here's an example of how to use the alert() function in JavaScript −

alert("Hello, World!");
// displays an alert box with the message "Hello, World!"

The alert() function takes a string as an argument, which is the message displayed in the alert box. The function will block the execution of the code until the user clicks the OK button.

Syntax

We can use the following syntax to call a JavaScript function in an alert box −

alert("message" + func(args));

The alert() function is a global function that is a method of the window object, that’s why we do not need the window.alert() to access it, although we can use it with the window object.

  • The message is a string passed as an argument to the alert() function.

  • In this case, the message is concatenated with the result of calling the func() function, using the + operator.

  • The func() function is a user-defined JavaScript function that accepts some arguments, which are passed as args.

Steps

The followings are the steps for calling a JavaScript function in an alert box −

Step 1 − Define the function you want to call in the alert box.

Step 2 − Call the function inside the alert() function.

Step 3 − If the function takes arguments, pass them as arguments to the function when you call it.

Step 4 − When you run the code, an alert box will be displayed with the message returned by the function.

Example

In the below example, we define a JavaScript function named greet and call this function inside the alert() function.

<html>
<body>
   <h2>Calling a JavaScript function in an alert box.</h2>
</body>
<script>
   function greet() {
      return "Hello, World!";
   }
   alert(greet()); // displays an alert box with the message "Hello, World!"
</script>

In the example above, the greet() function is called inside the alert() function, which causes the message returned by the greet() function to be displayed in the alert box.

Example

In the example below, we pass arguments to the function being called in the alert box.

<html>
<body>
   <h2>Calling a JavaScript function in an alert box.</h2>
</body>
<script>
   function greet(name) {
      return "Hello, " + name + "!";
   }
   alert(greet("John")); // displays an alert box with the message "Hello, John!"
</script>
</body>
</html>

In the example above, the greet() function accepts "John" as an argument. It is called inside the alert() function, which causes the message returned by the greet() function to be displayed in the alert box.

Example

<html>
<body>
   <h2>Calling a JavaScript function in an alert box.</h2>
</body>
<script>
   function add(x, y) {
      return x + y;
   }
   alert("The sum of 1 and 2 is " + add(1, 2));
   // displays an alert box with the message "The sum of 1 and 2 is 3"
</script>
</body>
</html>

In this example, the add() function is defined to take two arguments, x and y, and returns their sum. The function is then called inside the alert() function, and the result is concatenated with the string "The sum of 1 and 2 is ". The resulting message is then displayed in the alert box.

In summary, to call a JavaScript function in an alert box, you can use the alert() function and pass the function as an argument. You can also pass arguments to the function if needed.

Updated on: 05-Apr-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements