The code inside a JavaScript function will execute when it is invoked by an event or a function call.
A function’s code runs only when it is invoked, not when it is defined. While terms like “call,” “start,” or “execute” a function are common, this tutorial uses “invoke,” as JavaScript functions can be invoked without an explicit call.
function myFunction(a, b) { return a * b; } myFunction(10, 2); // Will return 20 |
In JavaScript, functions default to the global object. In browsers, this is the window object, so myFunction() and window.myFunction() are the same.
function myFunction(a, b) { return a * b; } window.myFunction(10, 2); // Will also return 20 |