JavaScript for Beginners - Functions (P: IX)

Functions

A JavaScript function is a block of code designed to perform a particular task.

 The main advantages of using a function;

-          Re-Use - define the code once and can re-use many times.

-          Use the same code many times with different arguments, to produce different results

Note: A JavaScript function is executed when “something” invokes or call it.

Defining a function

To define a function, use the “function” keyword, followed by a name, followed by a set of parentheses ().


Calling a function

To execute the function, you need to call it. To call a function, start with the name of the function, then follow it with the arguments in parenthesis.



 Note: Always remember to end the statement with a semicolon after calling the function.

Once the function is defined JavaScript allows you to call it as many times as you want to.

Comments