Allow us to write once and reuse whenever.

Naming functions

Name it what you want.

But name it something that is going to describe what it does.

Here is how you declare a function:

function doSomething(){



Simple but you get the point.

The function keyword is going to be first.

Then the name of our function.

If we have them, a list of our parameters inside the parentheses.

Then the function body inbetween the curly braces.

When we need to use our function, we call it with doSomething();

Variables and Scope

Local Variables

If a variable is declared within a function. It is only giong to be visible inside that function.

function showInnerText(){

let inner = "Inside the function."





Our result from the console.log(inner) is going to be: Uncaught ReferenceError: inner is not defined at "your code line"

Global Variables

If it's declared outside of any function, it's considered an outer/global variable.

let person = 'Annette';

function greet(){

let message = "Hello, " + person;




Gives us: Hello Annette!

Global variable can be modified as well.

let person = 'Annette';

function greetTwo(){

person = "Carol";

let messageTwo = "Hello, " + person + "!";



greetTwo(); //Hello, Carol!

console.log(person); //Hello Carol!

Person has now been modified by the function to Carol.

If you have an outer and local variable with the same name. The outer variable will be ignored, it will only be used if there is no local variable.

Try to only use global variables if you need that variable anywhere.


Also known as arguments.

It looks and works like this.

function getSports(league, team){

console.log("League was: " + league + ". " + "Team was: " + team + ".");


getSports("NBA", "Sixers");

//League was: NBA. Team was: Sixers.

The values for league and team were passed into and used by the function.

If a parameter is not given - the value becomes undefined.

Let's run the same League/Team example.

function getSports(league, team){

console.log("League was: " + league + ". " + "Team was: " + team + ".");



// League was: NBA. Team was: undefined.

We can give it a default value.

function getSports(league, team ="We're going to assume Sixers"){

console.log("League was: " + league + ". " + "Team was: " + team + ".");



getSports("NFL", "Cowboys");

// League was: NBA. Team was: We're going to assume Sixers.

League was: NFL. Team was: Cowboys.

Simple I know, but it can be as complicated as you'd like.

Run a function if a parameter is empty.

Whatever you need to do, to get the job done.

Return a value

It's going to give you back the value.

Helpful with calculations as it gives you the calculated value back.

function addTwo(a,b){

var c = a + b;

return c;



// 15

The return statement can be anywhere in the function.

When it's met, function stops and value is passed back to the code piece that called it.

Function expressions / arrows

I've showed Function declarations

function doSomething(){



But we can also declare a Function Expression

It looks like this:

let sayHey = function(){

alert ("Hey");


// Hey

The value of the function is store in sayHey.

sayHi is holding the value of the function that is created.

Create a function and put it in sayHey variable.