5

Is "function" a JavaScript type?

For example:

console.log(typeof alert) // returns function

Which suggests that "function" is indeed a type

However, in this ECMAscript documentation it says:

The ECMAScript language types are Undefined, Null, Boolean, String, Symbol, Number, BigInt, and Object”.

Could someone explain this to me?

Thanks in advance :)

4
  • 3
    Function is also an object. Commented Oct 11, 2020 at 9:41
  • @evolutionxbox so, why here is information about nine types (including "function")? developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures Commented Oct 11, 2020 at 9:46
  • @Peter That MDN page does not list Function as a separate ECMAScript type - that page says it's a "structural type" which is not the same thing as a fundamental ECMAScript type. Commented Oct 11, 2020 at 9:50
  • Same reason Array isn’t. They’re not fundament types Commented Oct 11, 2020 at 9:50

1 Answer 1

9

The behaviour of the typeof operator is documented in section 12.5.5, which has a table that answers your question.

It says (paraphrased):

  • Given the expression typeof val:
    • When val is an Object which implements the [[Call]] interface then typeof returns the string 'function'
    • When val is an Object which does not implement [[Call]] then typeof returns the string 'object'.

Thus, function is not a separate ECMAScript type, but is actually a specialization of the Object-type.


Do note that the information in the ECMAScript specification is very technical and narrowly specific and is intended primarily for implementors of ECMAScript engines and tooling - and while it is useful for developers using JS as a language, it is not intended to be beginner-friendly or serve as an introduction to the language - or to explain fundamentals.


The precise definition of a "function object" is explained in 6.1.7.2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.