JavaScript String at() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.at(index) returns one UTF-16 code unit by index. Positive indexes count from the start; negative indexes count from the end. It is read-only (ES2022) and never mutates the string.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.at. Start with this line.

JavaScript
string.at(index)
CallWhat it does
string.at(index)Primary signature.
string.at(...)Call with the arguments your case needs.

Built into JavaScript. Call at(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
string.at(index)

Parameters

ParameterDescription
indexRequired. Position; negative counts from the end
Bounds checkOptional. Needed when you must treat a miss specially

Return Value

Return ValueDescription
*The value returned by string.at().

JavaScript Examples

Example 1 — Positive Indexes

Read the first and fifth characters with at(0) and at(4).

JavaScript
const text = "CodeToFun";

console.log(text.at(0));  // "C"
console.log(text.at(4));  // "T"
Try It Yourself

How It Works

  1. Build the string. "CodeToFun" has indexes 0 … 8.
  2. Read the first character. at(0) returns "C" — same idea as text[0].
  3. Skip ahead. at(4) lands on the fifth character → "T".
  4. No side effects. The string is unchanged; at only reads.

Example 2 — Negative Indexes from the End

Use at(-1) for the last character and at(-2) for the one before it.

JavaScript
const text = "CodeToFun";

console.log(text.at(-1));  // "n"
console.log(text.at(-2));  // "u"
Try It Yourself

How It Works

  1. Start from the end. Negative indexes add length first.
  2. Last character. -1 + 9 = 8 → "n".
  3. One step earlier. -2 + 9 = 7 → "u".
  4. Why it helps. You avoid writing text[text.length - 1].

Example 3 — at(-1) vs charAt(-1) vs str[-1]

Only at() gives clean relative indexing from the end.

JavaScript
const text = "CodeToFun";

console.log(text.at(-1));                 // "n"
console.log(JSON.stringify(text.charAt(-1))); // ""
console.log(text[-1]);                    // undefined
console.log(text[text.length - 1]);       // "n"
Try It Yourself

How It Works

  1. Use at for relative access. at(-1) resolves to the last character → "n".
  2. charAt ignores negatives. charAt(-1) returns an empty string "".
  3. Brackets look up a key. text[-1] means property "-1", which does not exist → undefined.
  4. Rule of thumb. Want the last character? Prefer at(-1), never str[-1] or charAt(-1).

Browser & Runtime Support

String.prototype.at() is part of ES2022. Evergreen browsers shipped it in 2021–2022; Node.js added it in version 16.6.

✓ Baseline · ES2022

String.prototype.at()

Supported in Chrome 92+, Firefox 90+, Safari 15.4+, Edge 92+, and Node 16.6+. Not available in Internet Explorer.

95% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
String.at() Excellent

Bottom line: Safe for modern web apps and current Node LTS releases. For IE11 or very old browsers, feature-detect with if ("at" in String.prototype) and fall back to str[str.length - 1] or charAt(str.length - 1).

Frequently Asked Questions

at(index) returns the UTF-16 code unit at that position as a one-character string. Positive indexes count from the start (0 is first). Negative indexes count from the end (−1 is last). It does not change the string.
at() supports negative indexes and returns undefined when out of range. charAt() does not support negatives (charAt(−1) returns "") and returns "" for out-of-range indexes.
For non-negative indexes they usually match. The big difference is negative indexing: str.at(−1) returns the last character, but str[−1] is undefined because −1 is treated as a string property key.
It returns undefined when the resolved index is before 0 or at/after length — and it never throws.
Not always. at() returns one UTF-16 code unit. Many emoji use two code units (a surrogate pair). Prefer codePointAt() / String.fromCodePoint() or [...str] for full code points.
at() is ES2022. It works in Chrome 92+, Firefox 90+, Safari 15.4+, Edge 92+, and Node.js 16.6+. For older targets, use str[str.length − 1] or charAt(str.length − 1).

Next: charAt()

Compare the classic index method that returns an empty string instead of undefined.

charAt() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful