JavaScript Tutorial

adplus-dvertising
Function in JavaScript
Previous Home Next

Functions : are one of the fundamental building blocks in JavaScript. its is used repetitively in a program and it saves time while developing a web page.

To use a function, you must define it somewhere in the scope from which you wish to call it.JavaScript functions are used to perform operations. We can call javaScript function many times to reuse the code.

Function names can contain letters, digits, underscores, and dollar signs, A JavaScript function is defined with the function keyword

There are two main advantages of javaScript functions

  • Code reusability : We can call a function several times so it save coding.
  • Less coding : It makes our program compact. there are no need to write many lines of code each time to perform a common task.

How to declare a function in javasceipt

functionName(parameter1, parameter2, parameter3) {
    javascript code to be executed
}

The parentheses include parameter names separated by commas ,

The code which is executed by the function is placed in curly- brackets {} ,

Example :

<html>
<body>
<script> 
function prasun(){
alert("prasun is  a  MCA candidate"); 
} 
</script> 
<input type="button" onclick="prasun()" value="call function"/> 
</body>
</html>

Output:

Click on button
Previous Home Next