Functions

Declarations vs Expressions

There are actually two literal forms of functions.

function add(num1, num2) {
 return num1 + num2;
}
var add = function(num1, num2) {
 return num1 + num2;
};

For example:

var result = add(5, 5);

function add(num1, num2) {
 return num1 + num2;
}

Examples

--------------------------------------------------------------------------------

// they are hoisted to the top of the context where it was declared or to the global context

console.log(mult(1,3)); // 3, -- Funcion Multiplicar --

function mult(n1,n2) {
	console.log("-- Funcion Multiplicar --");
	return n1 * n2;
}

--------------------------------------------------------------------------------

En esta caso console.log devuelve algo debido al hosting de la funcion
Function hoisting happens only for function declarations because the function name is known ahead of time

            console.log(division(9,3)); // 3, -- Funcion Division --

            function division(n1,n2) {
                console.log("-- Funcion Division --");
                return n1 / n2;
            }
            
En este otro caso no es asi, por lo que la funcion "resta" no es una DECLARACION
si no mas bien una ASIGNACION y abra un error en consola

            var resultado = console.log(resta(10,5));

            var resta = function (n1,n2) {
                console.log("-- Funcion Resta --");
                return n1 - n2;
            }

--------------------------------------------------------------------------------

  
function add(n1, n2) {
  console.log("-- Funcion Suma 1 --");
  return n1 + n2;
}

 // ERROR!! 
 // el interprete me marca error por tener el
 // mismo identificador varias veces declarado
var add = function (n1, n2) {
  console.log("-- Funcion Suma 2 --");
  return n1 + n2;
}

console.log(add(1, 3));
console.log(add(3, 3));
var add1 = add;
console.log(add1(6, 3));



https://playcode.io/664865                  

--------------------------------------------------------------------------------

So this code causes an error:

// error!
var result = add(5, 5);

var add = function(num1, num2) {
 return num1 + num2;
};

References

THE PRINCIPLES OF OBJECT-ORIENTED JAVASCRIPT. Copyright © 2014 by Nicholas C. Zakas