Functions are one of the fundamental building blocks in JavaScript. A function is a set of statements that preforms a task or calculates a value. A function in JavaScript is somewhat similar to a procedure. A procedure is a set of statements that performs a task or calculates a value.

A function consists of the function keyword, the name of the function, a list of parameters as well as the javascript statements that define the function.

Below are a few examples:

I’m going to start by declaring a function using a function keyword. Then we have to name the function followed by parentheses. This is part of the syntax for declaring functions. After that add come curly braces.

What we add to the curly braces is what we refer to as the body of the function. within the curly braces is where all the statements that will define the logic within our application.

The logic for the function above should display a message on the console. Below we have a statement we closed with a semicolon. However, when a function is declared there doesn't need to be a semicolon added to the end because we are not declaring it like a variable.

We can call this function like so. We would call the name of the function followed by parentheses and a semicolon to indicate this is a statement.

Once the changes have been saved Samantha will appear on the console.

To make things a bit more interesting. Instead of displaying just the students name lets display a bit more. All we have to do is display hello in between the parentheses. This variable is referred to as the parameter.

The student function has one parameter called message. Message is only meaningful inside of the function. It will not be accessible outside of the function. Message is an input to the function. instead of displaying Samantha, we will display Good morning Samantha. To do so we will concatenate two strings.

To call the student function we need to pass a value for the variable or name parameter more accurately. Above I passed in Good morning. Notice that there is an empty space after morning. Goof morning is referred to as an argument. Good morning is an argument to the student function. message is the parameter of the student function. This will display:

{ Good morning Samantha }

We can reuse this function with a different input. All we have to do is change “Good morning ”. Below, notice that I changed it to “Good night”.

Once the changes have been saved both Good night Samantha and Good morning Samantha will be displayed in the console.

I hope you enjoyed :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store