Function in JavaScript

What you will learn here about JavaScript

  • What is function
  • JavaScript function declaration
  • JavaScript function example
  • JavaScript function parameters
  • JavaScript function return value

What is function

Function is block of code which is responsible for performing specific task. Function allows user to reuse the code. So function avoids code redundancy

JavaScript function declaration

In javascript function is declared using function keyword.

function example()
{
document.getElementById(‘result’).innerHTML=”This is just example”;
}

JavaScript function example

Example of function is given below

example();
function example()
{
document.getElementById(‘result’).innerHTML=”This is just another example”;
}

JavaScript function parameters

JavaScript example of passing parameters to function is given below.

example(“Jon Dow”);
function example(name)
{
document.getElementById(‘result’).innerHTML=name;
}

JavaScript function return value

JavaScript example of function returing value is given below. In JavaScript we don’t need to specify return type.

document.getElementById(‘result’).innerHTML=example();

function example()
{
return “Jon Dow”;
}


You may also like...