1

In the past I've been told: "Everything in Javascript is a Function". I'm curious how much truth there is to this. As an example I quote the Chrome JS Console:

> String
function String() { [native code] }
> Number
function Number() { [native code] }
> Object
function Object() { [native code] }
> Array
function Array() { [native code] }
> Function
function Function() { [native code] }

What about literals, operators?

  • 2
    The things you list are functions, but not everything. I've never heard anyone say that about JavaScript personally. http://stackoverflow.com/questions/3449596/every-object-is-a-function-and-every-function-is-object-which-is-correct – numbers1311407 Feb 08 '13 at 19:11
  • 1
    "Literal" refers to how certain values are represented in the source code and the parser knows how to interpret it. At this level, the concept of a function does not even exist. You are comparing apples with oranges in this point. Not sure what to say about "operators"... they are a different beast altogether and it does not make sense to include them in this comparison either. – Felix Kling Feb 08 '13 at 19:19
  • 1
    The things you list are all constructor functions. That is Javascript's way of defining a class (or something like a class). It does not mean that the objects (instances) constructed are themselves functions. E.g. in `var s = new String()`, `s` is not a function. – Stuart Feb 08 '13 at 19:23

2 Answers2

1

"Everything in Javascript is a Function"

Citation needed.

Your proof there actually proves that constructors are functions. And a constructor is the handle for the closest thing JavaScript has to a class.

Try this:

> typeof String
"function"

> var a = new String("testing123");
> typeof a
"object"

But really, functions are just a special kind of object.

So I would revise your statement to say instead:

Nearly everything in Javascript is an object, including functions

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • Perfect, thanks. I don't have a citation to my quote, it's just something that I seemed to remember someone saying, clearly it was wrong. –  Feb 08 '13 at 19:27
  • Instead of saying "Nearly everything in Javascript is an object, including functions", it's better to be more specific and say "Everything that is not a *primitive* is an object.", and this includes functions. Note that `typeof` is not always reliable, as `typeof null` is `"object"`, but in actual fact, it's a primitive. – d4nyll May 18 '17 at 09:00
0

Don't know about a function, but I've always heard everything in JavaScript is an object.

Stef
  • 29
  • 3
  • [(Not) Everything in JavaScript is an Object](http://blog.brew.com.hk/not-everything-in-javascript-is-an-object/). A function is an object in JavaScript. – d4nyll May 18 '17 at 08:59