Functions let you group a series of statements together to perform a specific task. If different parts of a script repeat the same task, you can reuse the function (rather than repeating the same set of statements. You must have seen function write() with document.write() in the earlier lessons. We were using these functions again and again, but they had been written in core JavaScript only once.
JavaScript allows us to write our own functions as well. This section explains how to write your own functions in JavaScript.
To create a function, you give it a name and then write statements needed to achieve its task inside the curly braces. You declare a function by using the keyword, function. You give the function a name followed by parethesis. Code inside the curly braces is the task that function does. Here's an example:
Function, sayHello() prints a message to the page. Having decalred the function, you can execute all of the statements inside the curly braces by calling the function. To call the function, you simple use the function name with parethesis (sayHello(); is written outside the function).