0

I had take date into a variable like below.

var todayDate = new Date();

After this I am assigning todayDate to another variable:

 var yesterdayDate = todayDate;

 yesterdayDate.setDate(yesterdayDate.getDate() - 1);

After this I am assigning todayDate to another variable:

 var next30daysDate = todayDate;
 next30daysDate.setDate(next30daysDate.getDate() + 29);

How should I keep todayDate constant for above two variables. in my case it is chnging for very variable.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Somasekhar
  • 61
  • 7
  • `= new Date(todayDate)` – evolutionxbox Sep 14 '22 at 13:55
  • 1
    @evolutionxbox - Sadly, that's not reliable in some JavaScript engines. (I know, right?) They'd want `= new Date(+todayDate)`. – T.J. Crowder Sep 14 '22 at 14:01
  • when you run `var yesterdayDate = todayDate` you only copy the reference, not the value. You have this other SO question explaining it https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – dinigo Sep 14 '22 at 14:09
  • @evolutionxbox - Glad to say it looks like it's been fixed at the spec level, so the caveat above would only apply to obsolete JavaScript engines. – T.J. Crowder Sep 14 '22 at 14:17

0 Answers0