JavaScript > Function

Function in JavaScript

How to create a function in JavaScript?


JavaScript codes can be directly placed inside the <script> tag or can be written inside the function so that they executes when that function is called.

To define a function, we use a reserved keyword function and write function name. The block of code that we want to execute inside the function is written under the opening "{" and closing curly braces "}".

<script>
             function AlertMe()
             {
             alert("I am displaying from alert method");
             }
</script> 

<input type="button" id="btnTest" onclick="AlertMe()" value="ClickMe!" />

In above code snippet, the alert message will be displayed only when the "ClickMe!" button will be clicked as AlertMe() function is being called on click of this button.

 Views: 7834 | Post Order: 10



Write for us






Hosting Recommendations