Skip to content

2.6 Summary

Let's summarize what we have learned in this section.

What is a Function?

A function is a block of code that can be re-used multiple times in the program.

A function may accept one parameter or more, and it processes the input parameters to produce a return value.

A function can be called by its name anywhere you want in your code.

function functionName(param1, param2,..., paramN){
  // Your function code

  return value;
}

//Calling the function
functionName(arg1, arg2, ..., argN);

DRY Principle

Do not Repeat Yourself (DRY) is a programming principle. DRY means that the programmer should not write the same code over and over again. Instead, he/she should find ways to reduce the amount of repetition in his/her code. For instance; he/she can use loops, and functions to avoid repetition.

Anonymous Functions

Anonymous function is a function that has no name.

var functionExp  = function(param1, param2,..., paramN){
  // Your function code

  return value;
}

//Calling the function
functionExp(arg1, arg2, ..., argN)

When use anonymous functions?

  1. When you need to add a function that will not be used anywhere else in the code.
  2. When you want to execute the function as soon as it is created.
  3. When you use them as IIFEs, and you want to make the function variables private.

IIFE

IIFE stands for Immediately-invoked Function Expression. It is a way to execute functions as soon as they are defined.

(function(){
  // Your function code ;
})();

Arrow Functions

Arrow functions are the same as anonymous functions, but they have different syntax. The arrow function syntax is: (param1, param2, param3) => // Function Statements

Type Coercion

Number(value) converts a given value to a number, if possible. If not, it returns NaN, which means Not A Number.

String(value) converts a given value to a string.

NaNrepresents not a number value in JavaScript. The type of NaN is a number. You can check if the value is NaN using isNaN(value)

JavaScript Scope

The scope is the place in the code that you can access a variable from.

The global scope is the area of the code that can be accessible from anywhere in your code.

The Local scope variables are declared within the function, and they are only accessible within the function.

Scope Chain: If this variable is used within a function, the JavaScript tries to find this variable in the local function scope. If it does not exist in the local scope, it searches for this variable in the global scope. If it does not exist in the global scope, it raises a ReferenceError.

Closures

A closure is a function that remembers the variables created in its enclosing function.

A closure function can access:

1. the variables declared within it.
2. the variables declared in the outer function.
3. the global variables.

The lexical scope of a function is where that function was defined. In the lexical scope; the function can access its own local variables, the parent function variables and the global variables.

function func1(num1){

  function func2(num2){
    return num1 * num2;
  }

  return func2;
}
//func2 has access to num1

Amazing! Let's learn about arrays now! Let's go to the next section!