functions and methods

JS Function vs Method

Function

  • Standalone, not attached to objects

  • Called directly by name: calculateTotal()

  • Defined with function declaration or expression

// Function example
function calculateTotal(items) {
  // ...
}

const calculateTotal = function(items) {
  // ...
};

Method

  • Function attached to an object or class

  • Called through object reference: object.doSomething()

  • this keyword refers to the containing object

References