• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • at()
      • Deutsch
      • Français
      • 日本語
      • 한국어
      • 中文 (简体)

    In this article

    • Try it
    • Syntax
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. fromCharCode()
    2. anchor() Deprecated
    3. big() Deprecated
    4. blink() Deprecated
    5. bold() Deprecated
    6. concat()
    7. fixed() Deprecated
    8. fontcolor() Deprecated
    9. fontsize() Deprecated
    10. italics() Deprecated
    11. link() Deprecated
    12. normalize()
    13. replace()
    14. small() Deprecated
    15. strike() Deprecated
    16. sub() Deprecated
    17. substr() Deprecated
    18. sup() Deprecated
    19. toString()
    20. trimEnd()
    21. length
  • Inheritance
  • call()
  • displayName Non-standard
  • arguments Non-standard Deprecated
  • caller Non-standard Deprecated
  • Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. toLocaleString()
    6. __proto__ Deprecated
    7. String consisting of the single UTF-16 code unit located at the specified offset. This method allows for positive and negative integers. Negative integers count back from the last string character.

  • Try it

    const sentence = "The quick brown fox jumps over the lazy dog.";
    
    let index = 5;
    
    console.log(`An index of ${index} returns the character ${sentence.at(index)}`);
    / Expected output: "An index of 5 returns the character u"
    
    index = -4;
    
    console.log(`An index of ${index} returns the character ${sentence.at(index)}`);
    / Expected output: "An index of -4 returns the character d"
    

    Syntax

    js
    at(index)
    

    Parameters

    index

    The index (position) of the string character to be returned. Supports relative indexing from the end of the string when passed a negative index; i.e., if a negative number is used, the character returned will be found by counting back from the end of the string.

    Return value

    A undefined if the given index can not be found.

    Examples

    Return the last character of a string

    The following example provides a function which returns the last character found in a specified string.

    js
    / A function which returns the last character of a given string
    function returnLast(str) {
      return str.at(-1);
    }
    
    let invoiceRef = "my-invoice01";
    
    console.log(returnLast(invoiceRef)); / '1'
    
    invoiceRef = "my-invoice02";
    
    console.log(returnLast(invoiceRef)); / '2'
    

    Comparing methods

    Here we compare different ways to select the penultimate (last but one) character of a String. Whilst all below methods are valid, it highlights the succinctness and readability of the at() method.

    js
    const myString = "Every green bus drives fast.";
    
    / Using length property and charAt() method
    const lengthWay = myString.charAt(myString.length - 2);
    console.log(lengthWay); / 't'
    
    / Using slice() method
    const sliceWay = myString.slice(-2, -1);
    console.log(sliceWay); / 't'
    
    / Using at() method
    const atWay = myString.at(-2);
    console.log(atWay); / 't'
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-string.prototype.at

    Browser compatibility

    See also

    • Polyfill of String.prototype.at in core-js
    • es-shims polyfill of String.prototype.at
    • String.prototype.indexOf()
    • String.prototype.lastIndexOf()
    • String.prototype.charCodeAt()
    • String.prototype.codePointAt()
    • String.prototype.split()

    Help improve MDN

    at() method of strings returns the character (one UTF-16 code unit) at an index, including negative indices for getting the character relative to the end of the string. Also known as the relative indexing method.","name":"String at()"}},"browserCompat":["javascript.builtins.String.at"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant