10

I just read this article on W3Schools about type conversion in JS. There stands:

There are 3 types of objects:

  • Object
  • Date
  • Array

This confused me because to my knowledge there isn't any difference between Date objects and any other object (typeof (new Date()) returns "object"). First I thought that it's special because it contains native code, but there are dozens of functions with native code.

Is this article wrong? Or could anybody tell my why the Date object is so extraordinary that it's considered a separate type of object?

Community
  • 1
  • 1
Aloso
  • 5,123
  • 4
  • 24
  • 41
  • 6
    You shouldn't really "trust" what `typeof` returns. After all, `typeof null` is `"object"` :) – Sergiu Paraschiv Apr 25 '16 at 09:09
  • 2
    Although W3Schools improved a lot recently, you should not trust the site as it still contains misleading information. Recommend to read MDN instead. – Raptor Apr 25 '16 at 09:11
  • 2
    There are a lot more [built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) than those three - there's nothing special about any particular object type - they are just API interfaces. – Emissary Apr 25 '16 at 09:12
  • Please use [Mozilla Developer Network - MDN](https://developer.mozilla.org/) for future references. – Praveen Kumar Purushothaman Apr 25 '16 at 09:21
  • Do not read w3schools. The information you posted here is completely wrong and is indicative of the quality of the site. Use the [*language specification*](http://ecma-international.org/ecma-262/6.0/index.html) or [*MDN*](https://developer.mozilla.org/en-US/docs/Web/JavaScript). – RobG Apr 25 '16 at 10:16

3 Answers3

17

Lemme tell you a basic thing. The articles in W3Schools are definitely outdated so you must not rely on it. Yes, when you give this in console:

typeof (new Date())

The above code returns object because the JavaScript has only a few primitive types:

You can check if it is a date object or not using:

(new Date()) instanceof Date

The above code will return true. This is the right way of checking if the particular variable is an instance of a particular type.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Except that *instanceof* does not work across frames. – RobG Apr 25 '16 at 10:18
  • @RobG Why does `frame` come in here? And what do you exactly mean? – Praveen Kumar Purushothaman Apr 25 '16 at 10:19
  • If a date is passed across frames, then it will not be an instance of the Date constructor in the second frame. See [*this question*](http://stackoverflow.com/questions/6473273/why-are-myarray-instanceof-array-and-myarray-constructor-array-both-false-wh/6473338#6473338), it's about arrays, but is true for any object created in another frame. – RobG Apr 25 '16 at 10:23
  • 1
    @RobG Mostly now-a-days frames are not being used buddy. Let's stick to the topic. – Praveen Kumar Purushothaman Apr 25 '16 at 10:26
  • @RobG Thanks for the insight though. Also, JavaScript is not supported on my Nokia 1110 Phone. So, this is like that. :) But nice to know. – Praveen Kumar Purushothaman Apr 25 '16 at 10:27
  • 9
    "Buddy"? Frames are used extensively by some very high traffic and high value sites. If you're going to claim "the right way" you should point out pitfalls, especially ones that are widely known and well documented. – RobG Apr 25 '16 at 11:07
  • @RobG I am not sure why frames are coming in this aspect. But yes, I agree with you. :) – Praveen Kumar Purushothaman Apr 25 '16 at 11:08
1

You might check an object for being an instance of a specific type also by verifying whether it has a method that is specific to the object type in question:

if (myobject.hasOwnProperty("getUTCMilliseconds")) {
    // myobject is a Date...

The same technique may help you identifying arrays in Javascript:
checking

typeof(myobject)  

yields "object", not "array" if myobject is really an array, so I use

if (myobject.hasOwnProperty("slice")) {
    // we are dealing with an array here ...
elwood
  • 129
  • 5
0

You can either use (as already suggested)

(new Date) instanceof Date

or

(new Date).constructor === Date

I honestly prefer to use the constructor option, but it's just a preference.

phaberest
  • 3,140
  • 3
  • 32
  • 40