Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you get string length and access characters?

JavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2026
6 min read

The length of a JavaScript string is its length property, which counts UTF-16 code units, and you read single characters with bracket notation (str[0]), at() or charAt(). For plain ASCII text the length equals the number of visible characters, but emoji and some accented letters take two code units, so the count can surprise you.

This guide covers the safe ways to read length and access characters, and when you need to count real characters instead.

Table of Contents

  • Getting the javascript string length
  • Accessing characters by index
  • Using at(), charAt() and charCodeAt()
  • Why emoji and accents change the length
  • Counting real characters
  • Looping over characters
  • Frequently Asked Questions
    • Is length a method or a property?
    • How do I get the last character of a string?
    • Why is the length of an emoji 2?
    • Can I change a character at an index?
  • Summary
  • Related Articles

Getting the javascript string length

length is a read-only property, not a method, so there are no parentheses.

javascript
javascript
Loading syntax highlighting...

Because strings are immutable, you cannot assign to length to shorten or extend a string. A common use is a guard before you process input:

javascript
javascript
Loading syntax highlighting...

Accessing characters by index

Strings are zero-indexed. The first character is at index 0 and the last is at length - 1.

javascript
javascript
Loading syntax highlighting...

Out-of-range bracket access returns undefined and does not throw. Assigning to an index does nothing (or throws in strict mode), because strings cannot be modified in place.

Using at(), charAt() and charCodeAt()

at() was added in ES2022 and accepts negative indexes that count from the end. It is the cleanest way to read the last character.

javascript
javascript
Loading syntax highlighting...

charAt() is older. It returns an empty string instead of undefined when the index is out of range.

javascript
javascript
Loading syntax highlighting...
MethodOut of rangeNegative indexReturns
s[i]undefinedundefinedcharacter
s.at(i)undefinedcounts from the endcharacter
s.charAt(i)""""character
s.charCodeAt(i)NaNNaNUTF-16 code unit number
s.codePointAt(i)undefinedundefinedfull code point number
javascript
javascript
Loading syntax highlighting...

Why emoji and accents change the length

JavaScript stores strings as UTF-16. Characters outside the Basic Multilingual Plane, such as most emoji, use two code units called a surrogate pair. length counts code units, not characters.

javascript
javascript
Loading syntax highlighting...

The same problem happens with a letter followed by a combining accent. The text "é" can be one code point (\u00E9) or two (e plus \u0301).

javascript
javascript
Loading syntax highlighting...

Counting real characters

The string iterator walks code points, so spreading a string or using for...of handles surrogate pairs correctly.

javascript
javascript
Loading syntax highlighting...

For what a user perceives as one character (a flag, a family emoji, a letter with accents), use Intl.Segmenter, available in current browsers and Node.js.

javascript
javascript
Loading syntax highlighting...

Use length for quick checks and storage limits, and use Intl.Segmenter when you show a character counter to users.

Looping over characters

A classic index loop is fine for ASCII data. For general text prefer for...of.

javascript
javascript
Loading syntax highlighting...

Frequently Asked Questions

Is length a method or a property?

It is a property. Write str.length, not str.length(); the second form throws a TypeError because a number is not a function.

How do I get the last character of a string?

Use str.at(-1). In older environments use str[str.length - 1].

Why is the length of an emoji 2?

length counts UTF-16 code units, and emoji outside the Basic Multilingual Plane take two. Use [...str].length for code points or Intl.Segmenter for visible characters.

Can I change a character at an index?

No. Strings are immutable, so build a new string with slice and concatenation, for example s.slice(0, i) + "x" + s.slice(i + 1).

Summary

str.length gives the number of UTF-16 code units, and str[i], at() and charAt() read characters, with at() supporting negative indexes. Out-of-range reads give undefined (or "" for charAt). For emoji and combining marks, count with the string iterator or Intl.Segmenter rather than length.

Related Articles

  • How do you create strings and escape characters in JavaScript?
  • What are template literals in JavaScript?
  • Learn JavaScript: Complete Study Roadmap, Beginner to Advanced
Share

Similar posts

  • How do startsWith and endsWith work in JavaScript?
  • How do you check if a string contains a substring?
  • How do you create strings and escape characters in JavaScript?
  • How do you solve FizzBuzz in JavaScript?
  • How do break and continue work in JavaScript loops?
  • How does the while loop work in JavaScript?
  • How does the for loop work in JavaScript?
  • How do you use a switch statement in JavaScript?