0

Here is a simple js code :

let day = 10;

let arrayMinutes = Array(day);
const d = new Date();
d.setHours(0);
d.setMinutes(1);
d.setSeconds(0);
arrayMinutes[0] = d;
console.log("arrayMinutes[0]", d, arrayMinutes[0], arrayMinutes);

const d2 = new Date();
d2.setHours(0);
d2.setMinutes(day);
d2.setSeconds(0);
arrayMinutes[day - 1] = d2;
console.log("arrayMinutes[day]", arrayMinutes[0], arrayMinutes[day - 1], arrayMinutes);

And the console logs :

enter image description here

Why arrayMinutes[0] evaluates to 0? According to me it should be equal to d.

9
  • What's day in your first line of code? Commented May 22, 2020 at 9:19
  • 1
    Setting day to 10, I don't get 0 at all, it logs a Date as expected: jsfiddle.net/khrismuc/cs0mnkfq Commented May 22, 2020 at 9:20
  • day is not defined. What is ``` arrayMinutes[day - 1] = d2;``` ??? and I suggest to use: ``` let arrayMinutes = []; ``` Commented May 22, 2020 at 9:22
  • 1
    @Lbro - If day is 10, the code doesn't do what you've said it does. Please update the snippet to accurately reflect what you're seeing. Commented May 22, 2020 at 9:30
  • 1
    I think I understood : the array is returned (return arrayMinutes) at the end of the function. And the function (calledinitX) is used to initialize a var, which, I suspect, is modified after. So returning the array is like returning a pointer, and therefore if afterward the data pointed are modified, the console.log shows the modified data. My suspiscions are correct to javascript gurus ? Commented May 22, 2020 at 10:06

3 Answers 3

2

The Array() constructor can be used in two ways:

  • Array(el0, el1, el2, ...)

Creates an array containing el0, el1, el2, .... This is equivalent to [el0, el1, el2, ...]

  • Array(count)

Creates an array with count elements, but the elements are uninitialized

If there's a single argument and it's an integer, it's taken to be the second form. So if day is an integer, you're creating an uninitialized array containing day elements.

Don't use Array(day), use [day] to create an array just containing the value day.

Sign up to request clarification or add additional context in comments.

Comments

2

Beware using the Array constructor. My guess is that your day variable at the beginning of the code is a number. When you do Array(number), it creates an array with no entries in it and a length matching that number:

const a = Array(10);
console.log(a.length); // 10
console.log(a[0]);     // undefined
console.log(0 in a);   // false

In fact, doing that is almost the only time the Array constructor is useful. Any other time, use an array literal:

const a = [10];
console.log(a.length); // 1
console.log(a[0]);     // 10
console.log(0 in a);   // true


The reason I say "beware" is that Array behaves differently based on what you give it:

  1. If you give it a single number argument, as above, it creates an empty array with length set to that number.

  2. If you give it a single non-number argument, it creates an array containing that value (with length = 1).

  3. If you give it more than one argument, it creates an array containing those values (even if the first one is a number).

This...changing...behavior is why you should avoid the Array constructor entirely unless you're intentionally creating an empty array of a specified length (usually just prior to filling it).

2 Comments

That said, you've said that arrayMinutes[0] is 0, not a date. I can't make that happen with your code.
This is strange. The all purpose of this function is to initialize an array so that the first item is the first minute of the current day and the last item is the last minute of the current day. (As it were not working,I tried with 10 instead of 1440). The console.log shows the first item to stay at 0, instead of being the first minute of the current day.
0

Maybe try with push

arrayMinutes.push(d);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.