O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting the Length of a String

var s = "One Ring"; s.length; //returns 8 var s2 = "To Rule\nThem All"; s2.length; //returns 16 because \n is only 1 character

String objects have a length attribute that contains the number of characters in the string. This includes the number of special characters as well, such as \t and \n. To get the length of the string, simply use the .length attribute on the string object.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required