×

Html & Html5

it training classes Logo

The World's best Software Development Study Portal

Js Function


If we want same type of operations to perform again and again than at that time javascript functions we can use. Some of the advantages are as follows :-

  • Less coading - We can call the function many times without writing any much code of lines.
  • Code reusability - Several times we can call our functions

Js Function syntax

Syntax

function functionName([arg1, arg2, ...argN]){

//code to be executed

}

Let's take an example:-

<html>

<body>

<script>

      function play(){

      document.write("I love to play Cricket");

}

</script>

<button type="button onclick="play()" >Click here </button> </body>

</html>

Output of this program

We can see for a fixed time we recapitulate our elements here.



Js Function arguments

We can also pass the argument inside the function .

Let's take an example:-

<html>

<body>

<script>

      function square(number){

      document.write("number*number");

}

</script>

<button type="button" onclick="square(4)" >Click here </button> </body>

</html>

Output of this program



Js Function with return value

We can use return keyword that returns a value which we can use in our program .

Let's take an example:-

<html>

<body>

<script>

      function triple(number){

      return number*number*number ;

}

</script>

document.write(triple(3)); </body>

</html>

Output of this program

27






JS training insitute | Best IT Training classes in Gurgaon | Web Designing Training Institute