FAQ: Arrays - The .length property

This community-built FAQ covers the “The .length property” exercise from the lesson “Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise The .length property

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

also

const objectives = ['Learn a new languages', 'Read 52 books', 'Run a marathon'];
console.log(objectives[1].length);

will return 13, because [1] is ‘Read 52 books’ and it’s 13 symbols long

7 Likes

Out of curiosity: is there a way one can use .length to get the total number of characters in an array?

For example:

const objectives = ['Learn a new languages', 'Read 52 books', 'Run a marathon'];

console.log(objectives.length); // console prints 3
console.log(objectives[0].length) // console prints 21

Is there a way to use .length to print the total number of characters in all three of the items in the array?

The simplest way to count all the characters is to join all the strings into one string.

objectives.join(' ').length
5 Likes

That’s exactly what I was looking for – thank you!

1 Like

I want to know the difference between the index of an array and the .length of an array?
why is that the index always start from zero to count and the .length start from one to count.

Hello, and welcome to the forums, @tera8025290123. The difference is quite straight forward. The length is simply the number of elements, using counting numbers. 0 length means the array is empty.

The index (or indices) correspond with the order of the array, counting from zero on up to the length, minus one. Zero is an index, that of the first element.

array[0]

It is a sequence, and its order never changes even if we shift around the elements. They all take the index of where they are moved to.

  0  1  2  3  4  5  6  7      <--  indices
[ 9, 8, 7, 6, 5, 4, 3, 2 ]
[ .  .  .  .  .  .  .  . ]    <--  length (count the dots)

The above array has a length of 8, and the last index is 7 so that array[7] is 2.

It’s the convention of this (JS) and many other languages. Quoting from Wikipedia,

… since the value of a pointer p used as an address accesses the position p + 0 in memory

which makes a lot of sense. I think of the index as being the offset from the starting point of the array. The first element has a zero offset.

@mtf

A quick question on this, since I just finished this exercise and wanted to try this out… When I log in this, the output is at 50. But I counted the characters and it comes to 48. Where does this take the other ‘2’ from? Could you please provide an answer to that? I tried but couldn’t reach a conclusion on that. Thank you :slight_smile:

Blockquote
const objectives = [‘Learn a new languages’, ‘Read 52 books’, ‘Run a marathon’];

console.log(objectives.length);

console.log(objectives[0].length);

console.log(objectives.join(’ ').length);

3
21
50

Did you count the two space characters that are inserted by .join()?

1 Like

Ah. That’s what I needed clarification on, if it was counting the spaces inserted by the .join() method. Thanks for the clarification.

1 Like

Join method joins the given string between those array elements…
Try to understand the below example, It will make you understand that what I am saying …

const objectives = ['Learn a new languages', 'Read 52 books', 'Run a marathon'];

console.log(objectives.length); //=> 3
console.log(objectives[0].length) //=> 21

console.log(objectives.join(''))//=> Learn a new languagesRead 52 booksRun a marathon
console.log(objectives.join("").length)// => 48

console.log(objectives.join(' '))//=> Learn a new languages Read 52 books Run a marathon
console.log(objectives.join(' ').length)// => 50

console.log(objectives.join(','))//=> Learn a new languages,Read 52 books,Run a marathon
console.log(objectives.join(',').length)// => 50

console.log(objectives.join('##'))//=> Learn a new languages##Read 52 books##Run a marathon
console.log(objectives.join('##').length)// => 52

2 Likes

I tested it! It worked and gave me 50 characters