xxxxxxxxxx
// Method 1: Using string indexing
str[str.length-1];
// Method 2: Using charAt()
str.charAt(str.length-1)
// Method 3: Using str.slice() function
str.slice(-1);
// Method 4: Using str.substr()
str.substr(-1);
xxxxxxxxxx
var str = "Hello";
var newString = str.substring(0, str.length - 1); //newString = Hell
xxxxxxxxxx
function test(words) {
var n = words.split(" ");
return n[n.length - 1];
}
xxxxxxxxxx
// Get last n characters from string
var name = 'Shareek';
var new_str = name.substr(-5); // new_str = 'areek'
xxxxxxxxxx
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
xxxxxxxxxx
var word = "linto.yahoo.com.";
var last = word.charAt(word.length - 1);
alert('The last character is:' + last);
xxxxxxxxxx
const str = "Hello World";
const lastCharacter = str[str.length - 1];
console.log(lastCharacter); // Output: d