In Node.js, we declare a function with the function keyword or the arrow syntax. Functions can call other functions—this is called structural programming.
We can pass functions as arguments to other functions. These are higher-order functions. Often, simpler code with for
-loops is faster.
Node programs are full of functions. When we see console.log
, this too is a function call—one we do not need to define ourselves.
multiplyBy2
, a function that receives an argument and returns that value multiplied by 2.function multiplyBy2(value) { // Multiply the argument by 2. return value * 2; } // Call the function. console.log("FUNCTION RESULT: " + multiplyBy2(4));FUNCTION RESULT: 8
We can create an anonymous function, and pass it as an argument to another function. Here we invoke forEach
, which loops over the elements of an array.
// Print 3 integer elements with an anonymous function. // ... Pass the function as an argument to forEach. [10, 20, 30].forEach(function(x) { console.log("VALUE IS: " + x); });VALUE IS: 10 VALUE IS: 20 VALUE IS: 30
A function can return a value. Often we use if
-statements to branch inside a function, and then return the correct value. A return is not required for all functions.
function getWord(n) { // Return string representation of number. if (n === 1) { return "one"; } else if (n === 2) { return "two"; } else { return "unknown"; } } console.log("GETWORD: " + getWord(1)); console.log("GETWORD: " + getWord(2)); console.log("GETWORD: " + getWord(1000));GETWORD: one GETWORD: two GETWORD: unknown
void
functionSuppose we try to use the return value of a function, but no return value is reached. We get the special value "undefined."
function getWord(n) { if (n === 1) { return "one"; } } // If no return statement is reached, we get undefined. console.log("RESULT: " + getWord(1000));RESULT: undefined
Early versions of JavaScript do not have arrow functions. But browser support for arrow functions has emerged. We have a left and a right side separated by the arrow operator.
// Use an arrow function and store the function in a variable. var pets = (cats, dogs) => (cats + dogs) // Call arrow function. var result = "I have " + pets(1, 2) + " pets."; console.log("ARROW FUNCTION: " + result);ARROW FUNCTION: I have 3 pets.
JavaScript functions are objects—this means they are often allocated and garbage-collected. Invoking the garbage collector tends to be slow in languages.
function single1() { console.log("RESULT 1"); } function single2() { console.log("RESULT 2"); } function combo(x) { if (x === 0) { console.log("RESULT 3"); } else { console.log("RESULT 4"); } } // Call functions. single1(); single2(); combo(0); combo(1);RESULT 1 RESULT 2 RESULT 3 RESULT 4
A JavaScript program can return multiple values from a function. An array or object (passed by a reference, or created in the function) can be used.
We can develop a lookup table of functions. Then we can skip if
-statements, and access functions based on a value. This requires some planning.
With functions we add structural designs to our Node programs. And with arrow functions, we have a simple syntax for lambda expressions.