Professional Documents
Culture Documents
Function
❖ What is Function/Definition
❖ Function Syntax
❖ Function Declaration
❖ Function Expression
❖ Anonymous function
❖ Variables
❖ Return statement
What is Function
A function is group of reusable code which can be called anywhere in your program. These
eliminate the need of writing same code again and again.
Function Syntax
……………………..
Function statements;
…………………...
}
Function Declaration
Example:-
Return sum;
document.write(result);
Function Expression
var Area=function(width,height)
return width*height;
document.write(Area(3,4));
Anonymous function definition
var anon=function()
alert(“I am anonymous”);
anon();
<script>
alert("I am anonymous");
anon();
setTimeout(function()
alert("hello");
},10000);
Return statement
The return statement is used to specify the value that is returned from the function.
So the function that are going to return value must use the return statement.
<head>
<script>
function MyReturn()
</script>
</head>
<body>
<script>
document .write(MyReturn());
variables
● GLOBAL variable
● LOCAL variable
A variable declared within a JavaScript function becomes LOCAL and can only accessed within
that function. You can have local variable with same name with different functions.Local
variable are deleted as soon as the function is completed.
GLOBAL variable:-
Variable declared outside a function become GLOBAL, all scripts and function on web page can
access it.Global variables are deleted when you close the page.