Definition
A JavaScript function is a block of code designed to perform a particular task.
A JavaScript function is executed when "something" invokes it (calls it).
|
Syntax
function name(parameter1, parameter2, parameter3) {
}
Types
1,function with return values and with parameters.
2,function with return values and with no parameters.
3,function with no return values and with no parameters.
4,function with no return values and with parameters.
|
function with no return values and with no parameters.
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>function1</title>
</head>
<body>
<script>
function sum(){
document.write("sum:"+(5+6));
}
sum();
</script>
</body>
</html>
|
Function with no return values and with parameters
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>function1</title>
</head>
<body>
<script>
function sum(p1,p2){
var p3=p1+p2;
document.write("sum:"+p3);
}
sum(1,2);
</script>
</body>
</html>
|
Function with return values and with parameters.
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>function1</title>
</head>
<body>
<script>
function sum(p1,p2){
var p3=p1+p2;
return p3;
}
document.write("sum:"+sum(5,2));
</script>
</body>
</html>
|
Function with return values and with no parameters
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>function1</title>
</head>
<body>
<script>
function sum(){
var p1=10;p2=20;
var p3=p1+p2;
return p3;
}
document.write("sum:"+sum());
</script>
</body>
</html>
|