Javascript Date Question 4 Compare

Introduction

What is the output of the following code:


let date1 = new Date();  
let date2 = new Date(date1.valueOf());   

console.log(date1 == date2);    //from w w w .ja v  a  2s .  c o  m
console.log(date1 > false);  

date2 = new Date(date2.valueOf() + 1);  

console.log(date2 > date1); 


false
true
true

Note


let date1 = new Date();  
let date2 = new Date(date1.valueOf());   

// Now that we have two identical, but distinct date objects we can compare them.

console.log(date1 == date2);    // false
console.log(date1 > false);    // true. false is converted to the number 0

date2 = new Date(date2.valueOf() + 1);    // We add one milisecond to the date

console.log(date2 > date1);   // true



PreviousNext

Related