Good beginner question 👍 — this one actually teaches a very important JavaScript concept.
You are pushing the SAME Date object reference into the array again and again.
👉 Date is an object, not a value type.
So when you do this:
MyDates.push(MyDate);
you’re not saving a new date — you’re saving a reference to the same object.
Later, when MyDate changes, all array entries point to that updated date, so they look identical.
Think of it like this 🧠
📌 You’re putting the same diary into the cupboard 4 times — not 4 different diaries.
Create one Date object → MyDate
Modify it in each loop (setDate)
Push same object into array each time
Result → array shows same final date everywhere ❌
You must create a new Date instance for each entry.
function Test_DateArray(){
let MyDate = new Date();
let MyDates = [];
for (let i = 1; i < 5; i++) {
MyDate.setDate(MyDate.getDate() + 1);
MyDates.push(new Date(MyDate)); // ✅ clone date
}
Logger.log(MyDates);
}
Avoid mutating the same date at all:
function Test_DateArray(){
let MyDates = [];
for (let i = 1; i < 5; i++) {
let d = new Date();
d.setDate(d.getDate() + i);
MyDates.push(d);
}
Logger.log(MyDates);
}
| Type | Behavior |
|---|---|
| Number / String | Copied by value |
| Object / Array / Date | Copied by reference |
So always remember:
Push objects by cloning, not reusing 🔁
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/984f5d5e-9d83-4d53-890f-0cf46d7e028fn%40googlegroups.com.
This is a very good beginner question, and it points to an essential JavaScript concept that also applies in Google Apps Script.
function Test_DateArray(){ let MyDate = new Date(); let MyDates = []; for (let i = 1; i < 5; i++){ MyDate.setDate(MyDate.getDate() + 1
); MyDates.push(MyDate); Logger.log(MyDates); } }
The issue is not the loop or setDate.
The issue is object references.
Date is an object, not a primitive value.
When this line runs:
MyDates.push(MyDate);
The array stores a reference to the same Date object, not a snapshot of its value.
So:
There is only one Date object
It is modified on each iteration
All array entries point to that same object
Result → all dates end up identical (the final value)
| Type | Behavior |
|---|---|
number, string, boolean | copied by value |
Object, Array, Date | copied by reference |
Create a new Date instance before pushing it into the array.
function Test_DateArray(){ let MyDate = new Date(); let MyDates = []; for (let i = 1; i < 5; i++){ MyDate.setDate(MyDate.getDate() + 1); MyDates.push(new Date(MyDate)); // clone
} Logger.log(MyDates); }
✔ Each array element now has its own Date object
✔ Dates remain independent
Cleaner and often safer:
function Test_DateArray(){ let MyDates = []; for (let i = 1; i < 5; i++){ let d = new Date(); d.setDate(d.getDate() + i); MyDates.push(d); } Logger.log(MyDates); }
✔ No shared references
✔ Easier to reason about
✔ Preferred in most Apps Script projects
Reusing objects inside loops
Expecting objects to behave like numbers or strings
Logging inside the loop (can hide the real issue)
💡 Tip: log after the loop when debugging arrays.
Never push mutable objects (Date, Object, Array) into arrays without cloning
Prefer immutability when possible
This concept appears everywhere: Sheets data, Calendar events, Drive files, JSON objects
This question is not boring at all — it’s foundational 👍