Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Function Invocation

The code inside a JavaScript function will execute when it is invoked by an event or a function call.

Invoking a JavaScript Function

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.

Invoking a Function as a Function

Example

function myFunction(a, b) {
  return a * b;
}
myFunction(102);           // 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.

Example

function myFunction(a, b) {
  return a * b;
}
window.myFunction(102);    // Will also return 20