Javascript Functions

Objectives

  • Understand the Use Case of Functions
  • Define & Invoke Functions With & Without Parameters

What is a function?

A block of code designed to perform a particular task

What are some routines you do in your everyday life?

How to make a function

// Function declaration
function sayLastName() {
  console.log('My last name is Gutierrez');
}



// Function expression
var sayName = function() {
  console.log('My name is Matt');
};

How to invoke a function

// Function declaration

function sayLastName() {
  console.log('My last name is Gutierrez');
}

sayLastName()

// Function expression
var sayName = function() {
  console.log('My name is Matt');
};

sayName()

Create your own function

Using the routine you thought of in the beginning of the class, make it into a function and console.log the steps of your routine

Customizing functions with parameters

var sayName = function(firstName) {
  console.log('My name is ' + firstName);
};

sayName('Matt')
// 'My name is Matt'

sayName('Mark')
// 'My name is Mark'

Add parameters to your function

Using the function you created, add some parameters

Javascript Functions

By Matthew Gutierrez

Javascript Functions

  • 192