MDN says that valueOf and getTime are functionally equivalent. Why have two functions that
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
Date.prototype.getTimemethod returns the number of milliseconds since the epoch (1970-01-01T00:00:00Z); it is unique to the Date type and an important method.The
Object.prototype.valueOfmethod is used to get the "primitive value" of any object and is used by the language internally when it needs to convert an object to a primitive. For the Date class, it is convenient to use the "time" attribute (the value returned bygetTime()) as its primitive form since it is a common representation for dates. Moreover, it lets you use arithmetic operators on date objects so you can compare them simply by using comparison operators (<,<=,>, etc).Note that you could implement the "valueOf" method for your own types to do interesting things: