DEV Community

San
San

Posted on

Get the last character of a string

const str = 'abcde';

// ✅ Get the last character of a string using charAt()
const last = str.charAt(str.length - 1);
console.log(last); // 👉️ e

// ✅ Get the last character of a string using slice()
const lst = str.slice(-1);
console.log(lst); // 👉 'e'

const lst2 = str.slice(-2);
console.log(lst2); // 👉️ 'de'

// ✅ Get the last character of a string using String.at()
const last_ = str.at(-1);
console.log(last_); // 👉️ e

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️
const last = [...str].pop()
Enter fullscreen mode Exit fullscreen mode