Javascript Theory
Functions in Javascript
A function is a block of javascript code that is defined once but may be executed, or invoked,any number of times A funciton can be used return a value, construct an object, or a mechanism to simply run code javascript functions are defined with the function keyword. Either function declaration or a function expression can be used
Javascript User Defined function :
It is possible to define your own functions.
Advantages:
- By using functions, we can avoid rewriting same logic or code again and again in the program.
- We can call functions at any number of times and from any place in a program.
- To improve the reusability.
- To reduce the size of the code..
To Create a function:
- Function name must start with a letter or an underscore but not with a integer.
- All functions can start with function keyword.
- It must not contain any spaces at function name.
function funtionname(parameter1, paramerter2, ..)
Syntax |
---|
{
//Function body } |
Example |
<script> function sum (num1, num2) { var result; result = num1 + num2 ; result result; } <script> |
---|