JavaScript

How to check the length of a string in JavaScript

October 31, 2021

How to check the length of a string in JavaScript

In this article we will learn an easy way to check the length of a string in JavaScript language, using a native function.

check the length of a string cover

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

We can use the string length for some check using the “if” statement and determine the course of our software depending on the received value

Or even some other problem that we need to solve on this data

And with JavaScript we have a native property to identify the length of a string

For this case we use the <length>

With this property we will receive a number value, with the number of characters in the string, learn how to use it:

let nome = "Matheus";
console.log(nome.length); // 7

And by that we solve the problem on how to see the length of a string in JavaScript! Cool huh? 😀

Also, another important use of length is to check the number of elements in an array.

The same way we use length in a string, we can use this property on the array

The return will be a number too, but with the number of elements of the array in question, check it out:

let linguagens = ['PHP', 'JavaScript', 'Python'];
console.log(linguagens.length) // 3

As JavaScript considers some entities as objetcts, like a string or array variable, we can access the length property to count letters or elements inside an array

Conclusion

In this article we learned about the possibility of returning the number of characters in a string applying the length property

Furthermore, the same property can check the number of elements that an array contains.

Want to learn more about JavaScript and other web technologies? Click here!

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