Basic functions
We have been calling functions for a while already. Remember prompt()
, console.log()
, push()
, and sort()
for arrays? These are all functions. Functions are a group of statements, variable declarations, loops, and so on that are bundled together. Calling a function means an entire group of statements will get executed.
First, we are going to have a look at how we can invoke functions, and then we will see how to write functions of our own.
Invoking functions
We can recognize functions by the parentheses at the end. We can invoke functions like this:
nameOfTheFunction();
functionThatTakesInput("the input", 5, true);
This is invoking a function called nameOfTheFunction
with no arguments, and a function called functionThatTakesInput
with three required arguments. Let's have a look at what functions can look like when we start writing them.
Writing functions
Writing a function can be done using the function
keyword. Here...