Array are represented by set of elements enclosed in square brackets.
length property of Arrays will return 5.
Arrays and extra commas(,)
Presense of extra commas(,) in arrays affects the array length. Any two adjacent commas in an array leaves an empty slot for unspecified element. Let's look at few examples.
And when you log x it will be
[5, <Empty element>, 6,7,8]
Here although only four elements are there in an array but the four commas (second empty ,) causes array length to be 5.
and when you log x it will be
Here again array has four elements and four commas(,) but the array length is 4 and not 5 this is because last comma is ignored.
Note: While using array iterating methods like Map this empty element is always skip.
It's always recommended to have extra comma at the end as it improves the readability of code when you are working on big arrays.
And also adding comma at end won't cause any merge conflict if multiple team members are adding elements to same array.
That's it for today!
Latest comments (0)