Warning: session_start(): open(/tmp/sess_b54b4b411d17c6472a831fe96a7a15b9, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to get the length of a string in JavaScript - LearnShareIT

How to get the length of a string in JavaScript

How to get the Length of a String in JavaScript

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:

Leave a Reply

Your email address will not be published. Required fields are marked *