You-dont-know-js: Up & Going - Chapter 2 - Values & Types: Should "function" be included in the summary list of types?

Created on 26 Aug 2016  ·  15Comments  ·  Source: getify/You-Dont-Know-JS

Given

var a = function () { };
typeof a;   // "function"

Should that be included in this section just for completeness' sake?

I know it's mentioned further down, but it might be good to put in that summary list given the importance of functions in JS.

for second edition

All 15 comments

The spec doesn't call function a type, so I believe it's better not to be inaccurate if it can be avoided. In the "Types & Grammar" book, I explain that function in the spec is actually a sub-type of object, called a "callable object". That detail is probably clutter for the absolute beginner reader of "Up & Going", though.

True, though I didn't mean adding it to the official list of built in types at the top, rather the list of examples. A reader may be naturally curious what type a function (and array, for that matter) would show.

Given the section which follows is just a series of examples, I thought it might be useful to show examples of functions (and arrays), but then make a quick point that array and function are actually sub-types of objects despite what they return when you use typeof.

E.g:

var a = function () { };
typeof a;                             // "function" -- actually a sub-type of object. See below. 

var a = [1, 2, 3];
typeof a;                             // "object" -- array is a special type of object. See below.

Or something to that effect.

But I understand if you feel that's too noisy this early in the series.

It's a quirk, almost a bug, that typeof function returns "function" instead of "object", at least in the strictest sense. It's a delicate balance but I prefer to be cautious about covering quirks and corner cases in such an early beginner book.

I'll keep this suggestion around to possibly handle during second edition. Thanks!

👍
I think this would be interesting. May be you could add it in the same way as you explain that the result of typeof null is weird. I don't think this is so awkard for a beginner.

Moreover for people who are curious and type "typeof function(){}", they won't suspect the book to be incomplete or outdated.

Omitting typeof "function" is more confusing. The following statement is not true:

The return value from the typeof operator is always one of six (seven as of ES6! - the "symbol" type) string values.

At the point where you discuss null as a special case of "object", it would make sense to note, in passing, that:

  • arrays are also a special case of "object" — you could expect their type to be "array" but it is "object", in spite of the fact that literal values of arrays look different than objects,
  • and so are functions — their type is "function" but they can be treated like a kind of object as well, i.e. to store properties like in an object.

The following statement is not true:

It absolutely is true.

I told you I was confused... The list above that statement has null which is never returned by typeof and omits function which is. Which does make six different values returned by typeof, right, just six unstated values (magic!).

The list above that statement has...

The list is labeled with "The following built-in types are available:" and is listing the types. It's not listing the string return values of typeof (notice there are no "s).

null is a type, but "null" regrettably is not a return value of typeof.

function is not (technically) a type, but "function" is a return value of typeof.

These inconsistencies are unfortunate, but they're how JS works.

I think that I get your point, from which stems my confusion: you are introducing the JavaScript type system, which is sadly not reflected properly in the typeof operator, while I incorrectly assumed that the typeof operator was the main topic.

Now that I understand better where you are coming from, I would suggest to move the discussion of the typeof operator out of the introduction of "Values & Types" and into its own section (which should obviously discuss the value function as well, but that's another point).

Fair suggestion. This thread is already marked as "second edition" to be re-visited during that work.

The return value from the typeof operator is always one of __six__ (__seven__ as of ES6! - the "symbol" type) string values.

Since typeof null returns object, is it not just __five__ string values (__six__, if you count ES6's symbol )?

  1. undefined
  2. string
  3. boolean
  4. number
  5. object
  6. symbol

I double-checked and still I can count only six return values in total. Am I missing something?

I apologize if this issue falls under the kind of typos that you advised not to bother with. Also, if this thread is not the right place for this issue(__if__ this is an issue at all), just let me know and I will create a new issue.

not a typo: typeof function == "function"

Oh!! True that! I guess I got confused because that statement immediately followed the example code and—not counting symbol—there were only five distinct return values within that example code.

I know you feel that adding another example like

var a = function(){};
typeof a;            // "function"

will be clutter for an absolute beginner, but, isn’t a little bit of clutter better than wondering what the seventh return value is and whether you are missing something? You could always assure the reader to not worry too much about it and that it will be discussed in detail later.

This is just a suggestion, though, based on my experience while reading the book.

Closing since "Up & Going" was replaced with the new "Get Started" book for second edition, so this issue is no longer applicable.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

williamvittso picture williamvittso  ·  3Comments

Beaglefoot picture Beaglefoot  ·  3Comments

laoshaw picture laoshaw  ·  3Comments

aszx87410 picture aszx87410  ·  3Comments

vineetpanwar picture vineetpanwar  ·  3Comments