String properties
Strings in JavaScript are sequences of characters, and the most important property they carry is length, the number of characters they contain. Strings also let you read individual characters by index, as if they were a read-only array. Understanding these basics, and how they interact with the fact that strings cannot be changed, prepares you for the string methods that come next.
The length property
length tells you how many UTF-16 code units a string holds, which for everyday text is the same as the number of characters, including spaces and punctuation. It is a property, not a method, so you write name.length without parentheses. It is commonly used to validate input, such as requiring a password of at least eight characters, or to limit a preview to a set number of characters.
const city = 'Bengaluru';
city.length; // 9
city[0]; // "B"
city[city.length - 1]; // "u"
city.at(-1); // "u"
city[20]; // undefined
city[0] = 'b'; // ignored: strings are immutable
console.log(city); // "Bengaluru"
const fixed = 'b' + city.slice(1); // "bengaluru", a new string
''.length; // 0
' hi '.length; // 6, spaces count
' hi '.trim().length; // 2
const bio = 'Full stack developer who enjoys building tools';
const preview = bio.length > 20 ? bio.slice(0, 20) + '...' : bio;
Accessing characters by index
Like arrays, strings start at index 0. Square brackets return the character at a position, and reading past the end gives undefined. The method charAt() does the same but returns an empty string for out-of-range indexes, while at() accepts negative numbers to count from the end. You can loop over a string with for...of, which visits one character at a time.
Strings cannot be changed in place
A string is immutable. Assigning to an index, as in city[0] = 'b', does nothing; in strict mode it throws a TypeError. Every string method that seems to modify text, such as toUpperCase(), replace() or trim(), actually returns a new string and leaves the original untouched. To keep the result, assign it to a variable. The length property is read-only too; setting it has no effect.
Where length goes wrong
Many emoji and some characters from other scripts are stored as two UTF-16 code units, so '👍'.length is 2, not 1. If you cut a string in the middle of such a pair with slice(), you get a broken character. When exact character counts matter, for instance in a "characters left" counter, spread the string into an array first: [...text].length counts code points instead.
Another common slip is writing text.length() with parentheses. Since length is a number, not a function, this throws "text.length is not a function".
Finally, remember that user input often contains leading or trailing spaces. Call trim() before checking length so a field filled only with spaces is treated as empty.
Where the methods come from
Apart from length and index access, everything you call on a string, such as includes() or split(), lives on String.prototype. JavaScript temporarily wraps the primitive string in a String object so those methods can be found. You almost never need to create new String() yourself, and doing so makes comparisons with === fail.
Points to keep
lengthis a read-only number, written without parentheses.- Index access reads characters but cannot change them.
- Use
[...text].lengthwhen emoji might be involved.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.