Basics

Function vs method — what's the difference, and why it isn't about how they're written

Illustration: the same coffee grinder — standing alone and built into a coffee machine

In Python you get a string's length like this: len(name). And you uppercase it like this: name.upper(). Why does the string sit in the parentheses in one case and before the dot in the other?

It's not a whim of the language's creators. That's exactly the difference between a function and a method. And it's simpler than it sounds: it's not about how they're written, it's about who they belong to.

In short: a method is a function with an owner

A function is a standalone, named piece of code. You give it data, it returns a result. It depends on no one.

function formatPrice(value) {
  return "$" + value
}

formatPrice(990)

A method is the same kind of function, but attached to an object. You call it through a dot, and it knows who it was called on.

const cart = {
  items: [990, 450],
  total() {
    return this.items.reduce((a, b) => a + b, 0)
  }
}

cart.total()

See this.items? The method fetches its owner's data by itself. You don't have to pass it in as a parameter. That's the whole point.

Picture a coffee grinder. You can buy a hand grinder — put it on the table, add beans, grind. That's a function. Or you can have one built into a coffee machine: it already knows which hopper to take beans from and where to send them. That's a method. Same mechanism; the difference is what it's bolted to.

The difference, point by point

  • Where it lives. A function — on its own, in a file. A method — inside an object or a class.
  • How it's called. A function — by name: formatPrice(990). A method — through a dot: cart.total().
  • Where it gets data. A function — only from the arguments in parentheses. A method — also from its object via this (in Python — self).
  • How to look it up in docs. A function — by name. A method — by the owner's type: "string methods", "array methods".
  • Whether it changes its owner. A function usually returns a new result and touches nothing. A method often changes its object: arr.push(5) appends to the same array instead of creating a new one.
  • The main trap. Functions don't have it. A method pulled off its object loses its owner: const f = cart.total — and f() crashes, because this is no longer cart.

So what about len(x) in Python?

Now the opening puzzle solves itself. upper() only makes sense for strings — so it's a string method. But length can be asked of a string, a list, a dictionary and plenty more. So len was made a general function that works with all of them.

JavaScript follows similar logic. Math.max(3, 7) looks like a method — and formally it is one, on the Math object. But Math has no data of its own. It's just a box someone put useful functions into. Such "stateless methods" essentially work like ordinary functions.

With arrays in JavaScript this is especially visible. arr.sort() is a method, and it reorders the elements in the array itself. The old order is gone. arr.toSorted() returns a sorted copy and leaves the original alone. Both are called through a dot, but the first changes its owner and the second behaves like a tidy function.

Here's the practical takeaway for reading other people's code. When you see a dot before a call, ask: does this use the data of the object on the left? If yes — it's a real method. If not — it's just a function on a shelf.

Which one to pick

A verdict, no fence-sitting.

  • Write a function by default. Especially in JavaScript and React. It's easier to check: same input — same result. It's easy to move to another file and reuse.
  • Write a method when data has behavior. A player who scores points. A cart that adds up its total. If you already have an object or class with state, actions on it naturally become methods.
  • Don't turn a function into a method for looks. If a "method" never touches this, it's a function hidden in an object for no reason.

It's worth asking AI directly: "make this a pure function, with no reference to this". Or the other way round: "make this a method of the Cart class". The model understands both.

Is a method also a function?

Yes. A method is a special case of a function: a function stored as a property of an object. That's why people mix the words up in conversation, and that's fine.

Why is arr.push() a method and not a function?

Because push changes the specific array it's called on. It needs an owner — without an array on the left there's simply nowhere to add the element.

What is a static method?

It's a method that belongs to the class, not to objects made from it: Date.now(). It has no object data of its own, so it behaves like an ordinary function.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →