// Get the last word of a string.
let str = "What I hate most in the world is fanaticism.";
// 1) The slice() method: without Considering the punctuation marks
str.slice(str.lastIndexOf(' ')); // => 'fanaticism.'
// 2) the spit() method():
let arr = str.split(' ');
arr[arr.length - 1]; // => 'fanaticism.'
// Considering the punctuation marks at the end of the string
str.match(/(w+)W*$/)[1]; // => 'fanaticism'
String test = "This is a sentence";
String lastWord = test.substring(test.lastIndexOf(" ")+1);
'abc'.slice(-1); // c
'abc'.slice(-2);
// Get last n characters from string
var name = 'Shareek';
var new_str = name.substr(-5); // new_str = 'areek'
str.charAt(str.length-1)
const myString = "linto.yahoo.com.";
const stringLength = myString.length; // this will be 16
console.log('lastChar: ', myString.charAt(stringLength - 1)); // this will be the string
Run code snippet