JavaScript tutorial-function


JavaScript function

A function is a JavaScript procedure containing code to perform a specific task.

-Declare a function

In JavaScript function must be declared with function keyword. After the function keyword, it is the name of the function and parentheses. See the example code below. The printHello, and sum functions are created. The printHello function does not return a value. The sum function return the toal value of a and b.

Example:

function printHello(){

document.write(“Hello”);

}

function sum( a, b){

return(a+b);

} 

 

-Calling a function

You can call function by writing the function name followed by parentheses. The values to pass to that function must be placed between the parentheses separated by comma.

Example:

printHello();

var result=sum(10,20);

Note: For function that returns value, you can declare a variable to get this value




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.