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
它不仅比vanilla JS更简洁、更明显,而且更安全,因为当输入是null或undefined时,它避免了Uncaught TypeError: Cannot read property X of undefined,所以你不需要事先检查:
// Will throw Uncaught TypeError if str is null or undefined
str.slice(-1);
str.charAt(str.length -1);
// Returns undefined when str is null or undefined
_.last(str);