As shown in the previous examples, JavaScript functions are defined using the function keyword.
Functions can also be defined using the built-in JavaScript Function() constructor.
const myFunction = new Function(“a”, “b”, “return a * b”); let x = myFunction(4, 3); |
You don’t necessarily need to use the function constructor. The example above is equivalent to writing:
const myFunction = function (a, b) {return a * b}; let x = myFunction(4, 3); |
In most cases, you can avoid using the new keyword in JavaScript. |