new function() javascript code example

Example 1: create function javascript

function myFunction(p1, p2) {
  return p1 * p2;   // The function returns the product of p1 and p2
}

Example 2: new function in javascript

// NOTE : Function defined using (Function CONSTRUCTOR) does not 
//        inherits any scope other than the GLOBAL SCOPE
 
var x=7;
function A(){
  var x=70;
   function B(){
     console.log(x);                // 70
   }
     let C = function(){
      console.log(x);              // 70
     }
	 		let D = new Function('console.log(x)'); // user caps F

	B();  // 70
    C(); // 70
    D();// 7 - Inherits always the GLOBAL scope
};

A();

Example 3: new function javascript

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