We will show you how to get the length of a string in JavaScript using three different ways: length and charAt(). This is a necessary knowledge when you work with arrays in Javascript.
Check if an array index exists in JavaScript
Using charAt()
The charAt() method is called on a string type; it takes no arguments and returns a character at the specific index
Syntax:
charAt(position)
Parameter:
- position: The index (position) of the character in the string
Basically, a string is a sequence of consecutive characters. If the index of the character you want to get is in the range of the string length (meaning that index < length of the string), then the string has not ended. However, the string will end when you find an index at which its character is empty. Therefore, you can use a loop to check whether the current index in a string returns a valid character or empty. For example, using a while loop:
let myString = "LearnShareIT"; // Get the length of a string let len = 0; while (myString.charAt(len)) { // If the current index is a character, increment len len++; } console.log(len);
Output:
12
Another example of using a for loop:
let myString = "LearnShareIT"; // Get the length of a string let len = 0; // If the current index is a character, increment len for (; myString.charAt(len); len++) { } { console.log(len); }
Output:
12
As you can see, we first initialized the len variable to 0, this variable represents the current length of a string. Whenever the value returned by charAt
() is not null, the length will be incremented. When the loop reaches its last character in the string, it is time that the charAt
() method will return an empty string and the loop will break. Therefore, the final value of the len variable is the length of a string you have declared.
let myString = "LearnShareIT"; // Get the length of a string let len = 0; while (myString[len]) { // If the current index is a character, increment len len++; } console.log(len);
Output:
12
Have you seen anything special here? We don’t use the charAt() method anymore, we access the string directly through the index (len) and we also get the same result as the first one. The logic behind this approach is very simple, as any valid index in a string will return a character instead of undefined. Therefore, the condition myString[len] in the while statement will check if the value at that given index is undefined or not. If it is undefined, it means that it has reached the end of a string and then stops the loop and prints the final length of a string. The same result also happens with a for loop:
let myString = "LearnShareIT"; // Get the length of a string let len = 0; // If the current index is a character, increment len for (; myString[len]; len++) { } { console.log(len); }
Output:
12
If you don’t want to get the length of a string like these approaches. There is another way that is simpler for you. Let’s see the next solution to know about it.
Using length
length is not a function in JavaScript, but it is a property that represents the length of a string. Therefore, all we have to do is accessing this attribute in the string using the dot operator:
let myString = "LearnShareIT"; // Get the length of a string let len = myString.length; console.log(len);
Output:
12
As you can see, we only need two lines of code to get the desired result we want. In fact, the logic behind this attribute is implemented the same as the first solution of using a loop to iterate through a string until the current character is no more valid.
Summary
We have known how to get the length of a string in JavaScript via two different approaches. We suggest you use the last solution (length property). If you have any questions, you can leave a reply to us and we will answer soon. Have a nice day!
Maybe you are interested:
- How To Get The Value Of A String After Last Slash Using JavaScript
- How To Get The First 3 Characters Of A String In JavaScript?
- Get the Last Word of a String in JavaScript
I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R