js get last two characters of string code example

Example 1: javascript string get last two character

var member = "my name is Mate";

var last2 = member.slice(-2);

alert(last2); // "te"

Example 2: js get last 4 digits

const id = "ctl03_Tabs1";
console.log(id.slice(id.length - 5)); //Outputs: Tabs1
console.log(id.slice(id.length - 1)); //Outputs: 1

Example 3: javascript get last n characters of string

'abc'.slice(-1); // c

Example 4: js get first 3 characters of string

const string = "0123456789";
console.log(string.slice(0, 2)); // "01"
console.log(string.slice(0, 8)); // "01234567"
console.log(string.slice(3, 7)); // "3456"

Example 5: find the last occurrence of a character in a string javascript

str.lastIndexOf(searchValue[, fromIndex])