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