1

Dmitry Baranovskiy's blog (creator of Raphael) says that there are only 6 types in Javascript:

Object, Number, String, Boolean, Null, and Undefined.

Each can easily be checked via (for example):

Object.prototype.toString.apply(undefined) //"[object Undefined]"

But what about:

Object.prototype.toString.apply(new Date()) //"[object Date]"

Object.prototype.toString.apply(/a/)//"[object RegExp]"

...and Array, Functions also...

Are they different? Why are they not mentioned?

PS:

Is it related to the less-specific check like:

>>typeof [] //"object" ?

6
  • First ones are primitives. The ones you mention are native objects: en.wikipedia.org/wiki/JavaScript_syntax#Native_objects They are all of type "Object" Commented Jun 7, 2013 at 8:54
  • @mishik so how many types are? 6? ( each of testing starts with "object"] Commented Jun 7, 2013 at 8:57
  • 2
    Have a look here: es5.github.io/#x8-toc. There are only 6 data types you can interact with. "An ECMAScript language type corresponds to values that are directly manipulated by an ECMAScript programmer using the ECMAScript language. The ECMAScript language types are Undefined, Null, Boolean, String, Number, and Object." Functions, arrays and regular expressions are all objects. Commented Jun 7, 2013 at 9:01
  • @FelixKling thanks. please consider convert this to an answer. (Q :are you saying that this method [Object.prototype.toString.apply] is not the right method for type checking ?) Commented Jun 7, 2013 at 9:10
  • @RoyiNamir Is your question answered for now? Then mark it as it is! :) Commented Jun 7, 2013 at 9:48

2 Answers 2

1

According to the ECMAScript Language Specification, 5.1 Edition, §8:

An ECMAScript language type corresponds to values that are directly manipulated by an ECMAScript programmer using the ECMAScript language. The ECMAScript language types are Undefined, Null, Boolean, String, Number, and Object.

From this definition it seems that functions, arrays and regular expressions are of the Object type.

This might seem strange for functions, since you can do something with them you can't do with other objects: You can call them. However, this characteristic is simply an internal property which not all objects have (also in §8):

Table 9 — Internal Properties Only Defined for Some Objects

[[Call]] Executes code associated with the object. Invoked via a function call expression.

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

Comments

0

Node, Element and HTMLElement are not types, just like Array, Date and RegExp they are just objects.

http://blogs.adobe.com/webplatform/2012/08/27/javascript-types/#comment-6038

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.