0

What happens in the following code for me to get 101?

let myArray = [1, 2, 3];

myArray[100] = "beauty"

console.log(myArray.length);

1

1 Answer 1

3

In JavaScript arrays are zero-based, so

let myArray = [1, 2, 3];

Has a length of three but the array element indices are 0, 1, and 2.

When you manually insert

myArray[100] = "beauty"

You put an element in the array with index 100 and then have elements 0-100 inclusive, so 101 array elements.

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

2 Comments

Thanks I think i get it. 99 + 0+ 1+1
@jessi. Correct. 0-100 is 101 items. Like 0-10 is 11 items

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.