JavaScript Functions

Basically, A group of statements is known as a function. We can identify function with this syntax function demo(){//function body}. We have to call it to perform any task or run statements in it.

We use functions to execute code whenever we want. The other benefit of function is saving memory because functions run whenever they are called and after presenting output its memory block becomes empty.

There are two types of function in JavaScript:

  • Predefined Functions and,
  • User-defined functions

Predefined Functions

These are also known as built-in functions. A predefined function is premade as its name suggests, just call it to use. You can not make changes in these. These can only be called. For example, toUpperCase(), toLowerCase(), etc.

User-defined Functions

When you make functions according to your needs. Then, this type of function is user-defined.

How To Call A Function?

This function executes when clicked on the button. This is the example of a user-defined function.

<html>
<body>

<button onclick="test()">Click On me to execute</button>

<script>
function test()
{
   alert("Welcome to jaischool.com");
}
</script>
</body>
</html>

This function will automatically execute as web page opens.

<html>
<body>

<h1 align="center">Welcome to jaischool.com</h1>

<script>
function test()
{
   alert("Welcome to jaischool.com");
}
test();
</script>
</body>
</html>

Leave a Comment