Hayk
Hayk

Reputation: 207

Is "Date" data type in JavaScript?

I read JavaScript book called "JavaScript: The Definitive Guide" and I see the following. "The Date type represents dates and times and supports rudimentary date arithmetic."

There is no "Date" in the list of primitive and object types, why "Date" is considered as type if it's just an object and in JavaScript if I am correct it's not possible to create our own types.

Is Date considered type in JavaScript?

Upvotes: 2

Views: 2985

Answers (2)

RobG
RobG

Reputation: 147453

There is no "Date Type", ECMASCript Types are:

  1. Undefined
  2. Null
  3. Boolean
  4. String
  5. Symbol
  6. Number
  7. BigInt
  8. Object

Note that the value returned by typeof does not necessarily match the Type of the value, e.g.

  1. typeof null returns "object"
  2. typeof someFun returns "function", where someFn is a an object that implements an internal call method (i.e. is a function)

ECMAScript has a built–in Date object that is a function that can also be called as a constructor. So:

typeof Date

returns "function" even though it's an Object Type, whereas using the typeof operator on a Date instance:

typeof new Date()

returns "object", as it's also an Object Type. The term "Date object" is commonly used to refer to a Date instance, and "Date constructor" to refer to the built–in Date object/function/constructor.

Upvotes: 6

Matt
Matt

Reputation: 5428

To illustrate @aravind's point:

console.log(typeof Date());
console.log(typeof new Date());

Upvotes: 0

Related Questions