This forum is now read-only. Please use our new forums! Go to forums
1.4 .length vs position in array
Easy question for any hacker. Could someone explain why an array with four properties has a .length of 4 but the last property number is 3?
I understand that the first property starts at 0 and counts +1 at each array as in: [0”diamonds”, 1”spades”, 2”clubs”, 3”hearts”]
Does javascript count to the 4th position, stop since there are no properties, and return 4?
Will I just have to memorize this as a rule?
Answer 4f663b8f9916fc000301ccbc
Answer 51a68d5b304609907c0029b6
0 votes
I don’t get this. Why does the length
equal to 4 and not 3?
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
5 comments
Because there are 4 items in the array.
But the index starts with 0.
Sure, the index does, and the 4 items are indexed as 0, 1, 2, 3.
Oh oh, right on! Thanks a ton. Spare me for my stupidness. I don’t know how could I have not thought about it lol. This is embarrassing.
Not to worry :)