Javascript: Functions 1

Imagine that you need to do a particular calculation over and over and over again in your program. Rather than paste that same piece of code numerous times, you could use a function. Functions allow you to reuse blocks of code in your program and are an integral part to many programming languages.

We have already seen functions in Javascript that are already defined for us: alert() and prompt().

If we want to define a function of our own we use the function keyword followed usually by the name of the function.

function sayHello(){
  alert("Hello World");

Then to call that function we use the following syntax.