Last Updated On By Khizer Ali
String is one of the most commonly used data types in any programming language. Making logics while writing code, we often come across the need to calculate the JavaScript String length.
In this short article, we will be focusing on how we can calculate the JavaScript string length using the length property of the String class.
str.length
It accepts no parameter and returns the JavaString String Length to which it is joined with the help of the dot operator.
Following is an example that shows the working of the JavaScript string length property.
var str1 = 'Hello World';
var str2 = 'a';
var str3 = '';
console.log("The length of "+str1 + " is: " + str1.length);
console.log("The length of "+str2 + " is: " + str2.length);
console.log("The length of "+str3 + " is: " + str3.length);
To run a JavaScript file, you can either add your JS code in HTML file under the <script> tag; or if you are saving it as a standalone file, then you need Nods.js runtime environment installed on your system.
In this brief article, we went through the JavaScript string length property and practically saw through an example how it can be used to return the length of a string.