1

I am having a lot of confusion about the Date keyword in javascript. The MDN article, in it's second line, states that Date is an object:

Date objects are based on a...

But typeof Date returns "function". Second thing is Date() -- with closed brackets -- should be a function because anything of type foo() is a function as per Douglas Crockford's book's chapter Grammar. Surprisingly enough, typeof Date() returns "string". Now if Date() is not a function then what is new Date()? A string constructor? Precisely my questions are:

  • What is Date? And how is it defined in javascript itself?
  • What is Date()? How is it different from Date? How is it defined in javascript itself? Why is it not a function?
  • What is new Date()? If Date() is a string then how can it act as constructor?
  • If we can instantiate new Date(), e.g. like new Date("October 13, 2014 11:13:00") then why can't we instantiate the original function Date() similarly as Date("October 13, 2014 11:13:00")?
user31782
  • 7,087
  • 14
  • 68
  • 143
  • “`Date` objects” refer to instance objects of the `Date` constructor function. `Date` is a function, `Date()` is the _result_ of the function call, which is a string. `Date` and `Date()` are different things. – Sebastian Simon Apr 23 '16 at 05:27
  • 1
    Did you search Stack Overflow? http://stackoverflow.com/questions/9584719/date-vs-new-date-in-javascript The fact that typeof Date returns "function" should be quite obvious: Date __is__ a function you can call, both as a function(and it will give you back a string) and as a constructor, prefixed with new. In this second case, as you noted, it will act as expected, building a new Date object. – Alberto Chiesa Apr 23 '16 at 05:28
  • `typeof Date()` is `string`..Of course.. It is a `typeof` value returned by `Date()`.. `Date` will hold function definition..That is the reason it is of type `function`.. `function foo()` is a function definition.. and `foo()` is invoking that function..which can return data of any primitive type.. _The new operator creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function_ – Rayon Apr 23 '16 at 05:42
  • Ty this: `function test() { return ''; } console.log(typeof test()); var t = new test(); console.log(typeof t);` – Rayon Apr 23 '16 at 05:46
  • @RayonDabre If I got you right then it means `typeof` returns the `return` value's _type_ of the function. – user31782 Apr 23 '16 at 05:49
  • @user31782, There is nothing fancy about it.. This is how all the programming languages work.. _"`typeof` returns the return value's type of the function"_ function name with `parenthesis"()"` will invoke the function and will return the value mentioned in `return` keyword.. or `undefined` if `return` is missing! – Rayon Apr 23 '16 at 05:51
  • The fancy thing to me here is that in javascript the return type can be _function_ -- which means `Date` returns some function. – user31782 Apr 23 '16 at 05:54
  • The return type in javascript can be anything, even a function! – koninos Apr 23 '16 at 05:56
  • @Konst, Any `primitive` type to be specific... – Rayon Apr 23 '16 at 05:56
  • @user31782, From docs, `typeof operand` => operand is an expression representing the object or primitive whose type "is to be returned" – Rayon Apr 23 '16 at 06:00
  • I got everything now. This is how the `Date` might be defined: `function Date2(today) { this.today = today; function curr_date() { return (today || "23rd april"); } return alert( curr_date() ); } Date2(); var x = new Date2("24th april"); ` – user31782 Apr 23 '16 at 06:05
  • In the actual version they might not use `alert()` but simply return `curr_date()` – user31782 Apr 23 '16 at 06:07
  • @RayonDabre _function_ is not a primitive type in javascript. But still we can return it. – user31782 Apr 23 '16 at 07:44
  • @user31782 Yes.. It is not a primitive type.. Right! – Rayon Apr 23 '16 at 08:32

2 Answers2

0

The typeof Date returns "function" because that is the type of the Date constructor. Using Date() without new keyword, will return date/time in string format. But if you create var foo = new Date(), typeof foo will be object.

koninos
  • 4,969
  • 5
  • 28
  • 47
  • So does it mean that `Date()` in a statement like function call, `Date();`is a _string_, but `Date()` in the statement `var foo = new Date()` is actually the `Date` constructor? I accept this but my question still remains. As Douglas's book `foo()` is the syntax of a function not of a string. If `Date` is a constructor then it is a function -- but where are it's closed brackets? How can the same thing `Date()` have different meanings -- Seems kinda a overloading with no parameters, strange. – user31782 Apr 23 '16 at 05:44
  • @user31782, You read him wrong... `foo()` is a function invocation... `function foo(){}` is a `function-definition`.. Do not blame _Douglas_ – Rayon Apr 23 '16 at 05:49
0

Thanks to Ryon Dabre for clearing some doubts. First source of confusion was typeof foo and type of foo() should be the same thing but they ain't. Suppose I have a function

function foo() {
        var len = 10;
        alert("hello");
        return 5;
     }

Now typeof foo will return "function" and typeof foo() will return "number". The statement foo is actually converted to foo's definition, e.g.

function bar(){
  return 6;
}
bar = foo;

bar becomes foo. Now bar has the return vale 5 not 6 -- just like if var x = 1; var y = 2; x = y; makes x 2. foo is not executed in the expression. Typing foo in the console returns "function foo()". That is foo is a variable of type function. So type of foo is function.

On the other hand whenever javascript sees foo(), it simply executes it. So in bar = foo() first foo() will be executed. Now what will be the value assigned to bar -- the value returned by foo(). Moreover type casting will also happen here. bar will no longer be a function. typeof bar will return "number". Actually typeof foo() and typeof bar both is "number", with the only difference being that the statement typeof foo() will first execute foo(). In javascript typeof foo() is translated to two statements:
1. execute foo.
2. typeof value_returned_by_foo


After conceptual clarification now lets discuss about Date(). Date is defined something like the following:

function Date2(today1) {
        var today;
        this.today = today1;            
        return (today || "23rd april");
    }

typeof Date2 is obviously "function". Since Date2 returns a string so typeof Date2() is "string". Date2() simply returns the string "23rd April". Date2("Monday") also returns 23rd April because today remains undefined. However when Date2 is used as constructor, today takes the value passed. E.g. var crux = new Date2("Saturday"); then crux.today returns Saturday.

user31782
  • 7,087
  • 14
  • 68
  • 143