[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/at [Back]  [Original]

String.prototype.at() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String.prototype.at()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2022 3.

String at() UTF-16 String . . .

In this article

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"

js
at(index)

index

(). . , .

UTF-16 String . undefined .

.

js
//      
function returnLast(arr) {
  return arr.at(-1);
}

let invoiceRef = "myinvoice01";

console.log(returnLast(invoiceRef)); // '1'

invoiceRef = "myinvoice02";

console.log(returnLast(invoiceRef)); // '2'

String ( ) . , at() .

js
const myString = "Every green bus drives fast.";

// length  charAt()  
const lengthWay = myString.charAt(myString.length - 2);
console.log(lengthWay); // 't'

// slice()  
const sliceWay = myString.slice(-2, -1);
console.log(sliceWay); // 't'

// at()  
const atWay = myString.at(-2);
console.log(atWay); // 't'

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.at


Web Proxy Viewer  |  New URL  |  Original Page