Why should a variable be defined as an object with the keyword new for a Date()? Over time I realized that if I do not define the variable as an object then I can not use JavaScript Get Date Methods for Date(), but why? When I write typeof Date()
It returns a string. If it returns a string then it's like writing var d = new String("October 13, 2014 11:13:00")
but if we use one of the methods this date will only work on
var d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();
Although both var d = new Date();
and var d = new String();
return the string, the method will only work on Date(). It's a bit confusing that the "October 13, 2014 11:13:00" string is saved in a variable that is an object, in both cases.
Why can I only call methods like getFullYear()
on a date created by new Date()
not by new String("October 13, 2014 11:13:00")
?