You are currently viewing How to Call a Javascript Function in HTML | Full Guide

How to Call a Javascript Function in HTML | Full Guide

How to Call a Javascript Function in HTML

Your created function only works if you call the function in javascript. So, In this article, I will tell you about how to call a javascript function in HTML.

What is calling a function?

Not only in JavaScript, but in any programming language, if we create a function and do not call it, the function will not run automatically. Only if we call it, the function will run. So you have to call the function with function name to execute it. You can run the function even when you click the button

Function Syntax

function functionname (arg1, arg2, ...arg n)
{
function operations
}

To create a function in javascript you first put the keyword “function” and then give your function name
You can create as many arguments as you want.
When you call the function, you must pass the same number of arguments as you passed when creating the function.
Put a set of parentheses () and write your function inside curly braces { }.

How to call a javascript function

You can call the function using the function name.

function dis(){
alert("HELLO")
};
dis();

In the above code, we have a function without argument, so we call the function without any parameters.

function add(num1, num2){
return num1+num2;
};

document.write(add(5,5));

In the above code, we have a function with 2 arguments, so we call the function with2 parameters.

Leave a Reply