Get a string's length with JavascriptGet a string's length with Javascript

Posted July 10th, 2009 in Javascript

This is one of those posts that looks at a really basic function: how to get the length of a string with Javascript. Yes this is a really basic function, but I found myself reaching for my O'Reilly Javascript book the other day because I couldn't remember if it really was as simple as String.length... In writing this post I know I will never forget it again!

Example:

The following example shows in an alert dialog the length of the string "this is a test":

var mystring = "this is a test";
alert( mystring.length );

And here's the above as a working example:

It's also possible to do this -> "this is a test".length <- which is what the example button above is doing in the onclick event handler.

Apologies to my regular readers for such a basic post, but sometimes I need to write these simple posts so I will always remember a particular function in the future.

Related posts:

Share or Bookmark

Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.

Subscribe or Follow

Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

Comments

blog comments powered by Disqus