Finding the Length of a String in JavaScript
by Amrit HallanByteswoth.com
Wednesday, 26th October 2005
When I used JavaScript to find the length of a string for the first time, I kept testing a function something like str.length(), whereas in JavaScript length is not a function, it is a property of the string object. So here is how you find the length of a string using length property of JavaScript:
<script language="javascript" type="text/javascript">
var str="When a dog bites it pains.";
alert(str.length);
</script>
var str="When a dog bites it pains.";
alert(str.length);
</script>
The function alert() in this case should throw up 26. So make a mental note that to calculate the length of a string str in JavaScript, it is str.length and not str.length().
Options:
Printer Friendly
About The Author:
Amrit Hallan is a freelance web developer. You can checkout his website at http://www.bytesworth.com. For more such articles join BYTESWORTH REACHOUT at http://www.bytesworth.com/br/default.asp or if you have a web dev related question then post it at http://www.business180.com/index.php
Amrit Hallan is a freelance web developer. You can checkout his website at http://www.bytesworth.com. For more such articles join BYTESWORTH REACHOUT at http://www.bytesworth.com/br/default.asp or if you have a web dev related question then post it at http://www.business180.com/index.php
