Web Development

Finding the Length of a String in JavaScript

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>

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().

About the author

Written by Amrit Hallan.

Amrit Hallan is a freelance web developer. You can follow the link below to checkout his website.

If you found this post useful you may also want to check these out:

  1. Learning the Basics of JavaScripting
  2. JavaScript Equivalent of PHP Explode Function
  3. The PHP Explode Function, Split a String by String
  4. Efficient Text Searching in Java: Finding the Right String in Any Language
  5. How to Hide your JavaScript with PHP!
  6. Change Form Field Values, On The Fly, with JavaScript