How to Get a String’s Length with Javascript

Written by Richi González Sunday, 10 February 2013

Hello,

Learn how to get a string’s length with a very simple Javascript property: length. This is really useful when you want to do, for example, field validations with JS; you evaluate the length and then do something.

The property is called length. The usage is pretty straight forward: text.length

Lets say you have a variable sample that contains I love Arvixe!, the code to alert the length, would be:

var sample = “I love Arvixe!”;

alert(sample.length); //To alert it

Now lets say you want to alert a success message if the length is greater than 5:

if(sample.length > 5) {
 alert("Success");
}

That’s it! If you have any question, please let me know so I can assist you!

Best Regards,

Richi

Owner of Juapo2Services

Looking for quality web hosting? Look no further than Arvixe Web Hosting!


Leave a Reply






− 2 = six