Functions in Node

Course Home | What are Methods? | Code Example(s)

So we’ve already seen examples of functions in the previous lessons, but let’s explore them in more detail

Let’s first look at how we declare a function. We’ve seen in previous lessons how we can declare functions but there are actually a few ways in which we can do it:

// I can declare a function by assigning it to a variable
var myFirstFunction = function(){
    console.log("Hello from function one");
}

// Or I can switch the function keyword and function name
function mySecondFunction(){
    console.log("Hello from function two");
}

// When I run this both functions will execute
myFirstFunction();
mySecondFunction();

We can also pass values into our functions through the use of parameters:

// This function has a parameter that can be used inside
// the function
function myThirdFunction(message){
  console.log(message);
}

// This means I can provide a value when I call the function
// and it will be used inside the function
myThirdFunction("Hello from function three");

Parameters can be Strings, Numbers and even other functions:

// This function declares has a parameter that is later
// in the function called as a function in it's own right
// (Hence the use of the brackets next the parameter name)
function myFourthFunction(callback){
    callback();
}

// So when we call the function and provide it with a
// second function it will be triggered
myFourthFunction(function(){
    console.log("Hello from function four");
});

Functions when executed can also return values through the keyword return like so:

// Functions can also return values to use later with the
// return keyword
function myFifthFunction(){
  var math = 10 * 10;
  return math;
}

var result = myFifthFunction();
console.log("10 * 10 equals: " + result);

Finally we should talk about a thing called scope. Scope is a design pattern that determines the access of a variable is based upon where it is declared. For example try running this code snippet:

// Let's declare a variable that has global scope
var messageOne = "Hello from global variable"

// Then let's declare a new function
function mySixthFunction(){
  // Let's declare a variable that has local scope
  var messageTwo = "Hello from local variable"

  console.log(messageOne);
  console.log(messageTwo);
}

// Then let's declare another function that uses
// the local scope variable from the previous function
function mySeventhFunction(){
  console.log(messageOne);
  console.log(messageTwo);
}

// Calling the first method will return both messages
mySixthFunction();
// Calling the second method will return an error when
// attempting to read the local variable
mySeventhFunction();

Next Lesson ➔

Mark Winteringham's photo
Author

Mark Winteringham

@2bittester

Mark Winteringham is a tester, toolsmith and the Ministry of Testing DojoBoss with over 10 years experience providing testing expertise on award-winning projects across a wide range of technology sectors including BBC, Barclays, UK Government and Thomson Reuters. He is an advocate for modern risk-based testing practices and trains teams in Automation in Testing, Behaviour Driven Development and Exploratory testing techniques. He is also the co-founder of Software Testing Clinic a community raising awareness of careers in testing and improving testing education. You can find him on Twitter @2bittester.

Comments