1

The documentation on MDN indicates that the syntax for Array.prototype.fill is:

Array.prototype.fill(value[, start[, end]])

The example

console.log([1, 2, 3].fill(4, 1, 1));         // [1, 2, 3]

in the documentation and my testing agrees with the commented answer.

However, I can’t see how that can be right. The parameters indicate that indexes 1 through 1 should be filled with 4. Index 1 has the value of 2, so I should have though that the result should be [1,4,3].

What is the correct interpretation of the parameters start and end?

1
  • the empty string string? Commented Mar 5, 2018 at 5:29

3 Answers 3

1

If you read polyfill code, you will see following code:

var k = relativeStart < 0 ?
  Math.max(len + relativeStart, 0) :
  Math.min(relativeStart, len);

// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ?
  len : end >> 0;

// Step 11.
var final = relativeEnd < 0 ?
  Math.max(len + relativeEnd, 0) :
  Math.min(relativeEnd, len);

// Step 12.
while (k < final) {

As you can see, it says while (k < final). In your case, both k and final holds same value and hence no mutation is made.


If you change the arguments to not being same, you will see the difference.

console.log([1, 2, 3].fill(4, 1, 2));


As suggested by skyboyer, same logic is shared in ECMA spec

Also suggested by skyboyer,

[...].slice(1, 1)

does not do anything. But

[...].splice(1,1)

"...".substr(1, 1)

mutates/ returns value. This is because:

@skyboyer for Array.splice, second argument is not index but count. For String.substr second argument is length and not index again. Hence both work just fine


References:

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

2 Comments

@skyboyer essentially, bounds of a loop should be different. If I say, you have to loop from one to one, there is no need for loop. In my understanding, the implemented logic makes sense.
@skyboyer for Array.splice, second argument is not index but count. For String.substr second argument is length and not index again. Hence both work just fine
0
console.log([1, 2, 3].fill(4, 1, 1));

First argument 4 is value to fill with. Second argument 1 is starting index (where to start from). Third argument 1 is ending index which is exclusive.

So if you do: console.log([1, 2, 3].fill(4, 0, 3));

It will start filling [1,2,3] array starting from index 0 (value 1) and overwriting all values up to the index of 2 since we said 3 is exclusive.

So result will be: [4, 4, 4]

3 Comments

I think the key word here is exclusive. I get that. Do you have a reference for this? MDN doesn’t make any mention of this.
@Manngo You're right, they don't, I mean I'm not JS programmer really, my background is Java and the common case with methods where you have these start and end values is that starting value is inclusive and ending value is exclusive.
OK, I get that too. The documentation for Array.prototype.slice specifically mentions not included, which fill does not. Thanks.
0

In simple words

console.log([1, 2, 3].fill(4, 1, 1));  

second and thid parameters are nothing but the length of the array to be filled that is calculated by subtraction in your case 1-1 is 0 so it will not fill any thing if you want to fill only second one you have to use

console.log([1, 2, 3].fill(4, 1, 2)); 

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.