String.prototype.at()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2022ë 3ìâ©.
String ê°ì at() ë©ìëë ì ì ê°ì ë°ì ì§ì ë ì¤íì
ì ìì¹í ë¨ì¼ UTF-16 ì½ë ì ëì¼ë¡ 구ì±ë ìë¡ì´ Stringì ë°íí©ëë¤. ì´ ë©ìëë ìì ì ìì ìì ì ì를 모ë íì©í©ëë¤. ìì ì ìë 문ìì´ì ë§ì§ë§ 문ìë¶í° ê±°ê¾¸ë¡ ì¸ì´ ìì¹ë¥¼ ì§ì í©ëë¤.
ìëí´ ë³´ê¸°
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"
구문
at(index)
매ê°ë³ì
index-
ë°íí 문ìì´ ë¬¸ìì ì¸ë±ì¤(ìì¹)ì ëë¤. ìì ì¸ë±ì¤ë¥¼ ì ë¬íë©´ 문ìì´ ëììë¶í°ì ìëì ì¸ë±ì±ì ì§ìí©ëë¤. ì¦, ììê° ì¬ì©ëë©´ ë°íëë 문ìë 문ìì´ì ëììë¶í° ê±°ê¾¸ë¡ ì¸ì´ ì°¾ìì§ëë¤.
ë°í ê°
ì§ì ë ìì¹ì ìë ë¨ì¼ UTF-16 ì½ë ì ëì¼ë¡ 구ì±ë Stringì ë°íí©ëë¤. 주ì´ì§ ì¸ë±ì¤ë¥¼ ì°¾ì ì ìì¼ë©´ undefined를 ë°íí©ëë¤.
ìì
>문ìì´ì ë§ì§ë§ 문ì ë°í
ë¤ì ìì ë 주ì´ì§ 문ìì´ìì ë§ì§ë§ 문ì를 ë°ííë í¨ì를 ì ê³µí©ëë¤.
// 주ì´ì§ 문ìì´ìì ë§ì§ë§ 문ì를 ë°ííë í¨ì
function returnLast(arr) {
return arr.at(-1);
}
let invoiceRef = "myinvoice01";
console.log(returnLast(invoiceRef)); // '1'
invoiceRef = "myinvoice02";
console.log(returnLast(invoiceRef)); // '2'
ë©ìë ë¹êµí기
ì¬ê¸°ìë Stringì ëìì ë ë²ì§¸(ë§ì§ë§ìì íë ì) 문ì를 ì ííë ë¤ìí ë°©ë²ì ë¹êµí©ëë¤. ìëì 모ë ë°©ë²ì´ ì í¨íì§ë§, ì´ë at() ë©ìëì ê°ê²°ì±ê³¼ ê°ë
ì±ì ê°ì¡°í©ëë¤.
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® 2026 Language Specification > # sec-string.prototype.at > |