How to – Define JavaScript Functions

There are 3 ways you can define JavaScript Functions:

1- You can use “Function” constructor

var total = new Function('a', 'b', 'c', 'return a + b + c;');
alert(total(1,1,1));
-> alerts '3'

2- You can use function declaration
(This is the one I use the most, might have advantages while browsing your external .js files)

function total(a, b, c)
{
return a + b + c;
}
alert(total(1,1,1));
-> alerts '3'

3- You can use function expression

var total = function (a, b, c) {return a + b + c;}
alert(total(1,1,1));
-> alerts '3'

Hope this helps someone.

Good luck,
Serdar

Leave a Reply

Your email address will not be published. Required fields are marked *


Warning: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given in /home/biginf5/public_html/wp-includes/wp-db.php on line 3215