JavaScript

Check the number of elements in an array with JavaScript

November 17, 2021

Check the number of elements in an array with JavaScript

In this article we will learn an easy way to check the number of elements in an array, applying the JavaScript language.

Check the number of elements in an array cover

What’s up programmer, okay? Let’s learn something new?

There are many situations in which we need to check the number of elements in an array

You can even define how a system will proceed, according to this answer.

So to solve this problem, JavaScript has native functionality.

Through the length property, we can identify how many elements there are in the array, check it out:

let nomes = ['Matheus', 'João', 'Pedro'];

console.log(nomes.length); // 3

Notice that a number is returned to us, with the number of elements

And then our problem is solved

Curiosity: number of characters in the string

Another interesting point of the length property is that it works for strings as well.

And in this case, we will receive the number of letters that the word or phrase contains, like this:

let frase = "Como verificar quantidade de elementos no array";

console.log(frase.length); // 47

Notice that the return is a number as well

Conclusion

We learned that we can determine the number of elements in an array with the length property

That will returns us a number, with the number of elements

The property works for strings too, and in this data type it checks the number of characters

Want to learn more about JavaScript? Click here!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x