-1

I'm just trying to understand the difference between functions and methods, and parseInt() only brought more confusion to the table.

According to mdn web docs, it's a function:

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

While - for example - w3schools states it to be a method:

The parseInt method parses a value as a string and returns the first integer.

Which one is it, then - a function or a method? And for future cases, how can I intuitively 'tell the two apart'?

Kotie77
  • 3
  • 2
  • 3
    If you find a conflict between MDN and w3schools, trust MDN. w3schools is better than it used to be, but still has quality and reliability issues. – T.J. Crowder Feb 13 '23 at 08:18
  • https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function maybe it can helps you. – Álvaro Feb 13 '23 at 08:18

1 Answers1

0

Quoting the spec:

A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, BigInt, String, and Symbol; an object is a member of the built-in type Object; and a function is a callable object. A function that is associated with an object via a property is called a method.

Since parseInt isn't associated with an object, it's a function, not a method.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • 1
    *Technically*, `parseInt` is a function property of the global object -- https://tc39.es/ecma262/multipage/global-object.html#sec-parseint-string-radix :-) I still wouldn't call it a method, though, not least because it doesn't use any information from the object on which it's called, which I would consider a missing part of the definition the spec uses. – T.J. Crowder Feb 13 '23 at 08:20
  • @T.J.Crowder I knew someone would say that. ;-) – AKX Feb 13 '23 at 08:21