-1

I'm a bit confused about the function data type. So is 'function' in Javascript language really a data type?

MDN docs consider it as a data type:

But on the ECMAScript site I couldn't find such a thing: ECMAScript® 2020 Language Specification

4
  • You can try it yourself easily enough: typeof function(){} // "function" If that's not what you mean then I'm not entirely sure what you're asking here. Commented Apr 15, 2021 at 0:05
  • 2
    I think you need to define what you mean by "data type" Commented Apr 15, 2021 at 0:06
  • Sure it is. It can be passed, stored, set, etc. like any other data type. When it is evaluated it returns the result of itself, just like when you evaluate an integer or a string they just return themselves. In Javascript, functions are derived from Objects, which inherently makes them just as much a type as an Object. Commented Apr 15, 2021 at 0:07
  • see this discussion which touches on the behavior of typeof Commented Jun 21, 2024 at 5:40

1 Answer 1

2

It is a type, in a way:

function

member of the Object type that may be invoked as a subroutine

In addition to its properties, a function contains executable code and state that determine how it behaves when invoked. A function's code may or may not be written in ECMAScript.

Functions inherit from objects; you can set arbitrary keys and values on functions (though you usually shouldn't, since it's weird). But functions are a special kind of object which can be invoked and whose typeof evaluates to function.

The page you're looking at doesn't look very precisely written. You might be better served by looking at a different site for an introduction, such as MDN.

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

1 Comment

We have arrays. It's a data structure in the language but it's not a separate data type. In ECMAScript specification, we don't have a Function type or Array type either.

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.