JavaScript String Length Function
Learn the javascript string length function with example codes. The length function works only to find the length of the string only. If we try to find the length of an integer or float, then it returns “undefined”. This function calculates the space in the string and returns the length value.
Example code:
var str1= "Hello world";
var str2 = 17.3;
document.write(str1.length);
document.write(str2.length);
Output:
- Here, we have created a variable called str1 and str2. We assigned the string value to str1 and the integer value to str2.
- We know that the length function allows only string variables. If we pass an integer then it returns “undefined”.
- See, the above output the str1 length is 11 and the str2 length is undefined.