1

As in the title, why

typeof +

doesn't give "function"?

user1544128
  • 583
  • 4
  • 20

2 Answers2

13

+ is an operator, it's not a function. typeof + is therefore a syntax error.

rid
  • 61,078
  • 31
  • 152
  • 193
  • 3
    While this answer is perfect, it does lead to another question: Wouldn't it be nice if `+` **were** a function... `var total = list.reduce(+, 0)`. Or even nicer, if we had automatic currying/partial application and we defined `reduce = function(fn, start, list)` so that we could just do `sum = reduce(+, 0)`. No, that's just **[crazy](http://en.wikipedia.org/wiki/Functional_programming)** **[talk](http://www.haskell.org/haskellwiki/Haskell)**! – Scott Sauyet Jan 23 '14 at 16:03
  • yep, after playing with haskell and scheme, since javacript was presented to me as kinda similar to them, I was hoping for it. Apparently I was mislead. – user1544128 Jan 23 '14 at 16:06
  • @user1544128, JavaScript has many things in common with lisp languages, but this is not one of them. – rid Jan 23 '14 at 16:06
  • @ScottSauyet, you can easily make a `var sum = function(a, b) { return a + b; }` function and use it the same way you would use `+` if it were a function. – rid Jan 23 '14 at 16:10
  • @rid: Yes, I understand. I've been working on **[Ramda](https://github.com/Crosseye/ramda)**, a functional programming library for JS to give me some of these capabilities I'd like to have. So I know that I can create them if I like. And my versions of `foldl`/`foldr`/`reduce` are automatically curried, and I do have an `add` function, and `sum` is defined in terms of `reduce` and `add`. But there's a big part of me that would love it if this were in the language instead... – Scott Sauyet Jan 23 '14 at 16:18
2

typeof only returns types of objects (pretty much everything in JS) and primitives.

See the MDN documentation for typeof.

As rid points out, + is an operator and not an object or primitive.

Community
  • 1
  • 1
Josh Harrison
  • 5,927
  • 1
  • 30
  • 44