EDIT: As others have pointed out, use slice(-5) instead of substr. However, see the .split().pop() solution at the bottom of this answer for another approach.

Original answer:

You'll want to use the Javascript string method .substr() combined with the .length property.

var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"

This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string.

You can also use the .slice() method as others have pointed out below.

If you're simply looking to find the characters after the underscore, you could use this:

var tabId = id.split("_").pop(); // => "Tabs1"

This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want).

Answer from Jamon Holmgren on Stack Overflow
Top answer
1 of 16
1429

EDIT: As others have pointed out, use slice(-5) instead of substr. However, see the .split().pop() solution at the bottom of this answer for another approach.

Original answer:

You'll want to use the Javascript string method .substr() combined with the .length property.

var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"

This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string.

You can also use the .slice() method as others have pointed out below.

If you're simply looking to find the characters after the underscore, you could use this:

var tabId = id.split("_").pop(); // => "Tabs1"

This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want).

2 of 16
1084

Don't use the deprecated .substr()!

Use either the .slice() method because it is cross browser compatible (see issue with IE). Or use the .substring() method.

The are slight differences in requirements, which are properly documented on: String.prototype.substring()

const id = "ctl03_Tabs1";

console.log(id.slice(-5)); //Outputs: Tabs1
console.log(id.slice(-1)); //Outputs: 1

// below is slower
console.log(id.substring(id.length - 5)); //Outputs: Tabs1
console.log(id.substring(id.length - 1)); //Outputs: 1
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
OneCompiler
onecompiler.com › qna › 3xnp8wgek › -javascript-how-to-select-last-three-characters-of-a-given-string
[Javascript] How to select last three characters of a given string? - Q&A - OneCompiler
You can use either substring() or slice() methods to select only last three characters of a given string. Check the usage as shown below: let str= 'This is a test message' console.log(str.slice(str.length-3, str.length)); // Outputs age console.log(str.substring(str.length-3, str.length)); ...
Discussions

javascript - access the last three Characters of the value in Jquery - Stack Overflow
I have a value "319CDXB" everytime i have to access last three characters of the Strring how can i do this . Usually the Length varies all the time .Everytime I need the last characters of the Str... More on stackoverflow.com
🌐 stackoverflow.com
Bracket Notation to find the last character in a string
I don’t understand why this code gives a letter a not a number as a result/in the console: var firstName = "Ada"; var lastLetter = firstName[firstName.length - 1]; I question this because this code gives me a number: var firstName = "Ada"; console.log (firstName.length); More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
September 1, 2021
Remove last 3 characters of string or number in javascript - Stack Overflow
I'm trying to remove last 3 zeroes here: 1437203995000 How do I do this in JavaScript? I'm generating the numbers from new date() function. More on stackoverflow.com
🌐 stackoverflow.com
javascript - How can I get the last character in a string? - Stack Overflow
If I have the following variable in javascript var myString = "Test3"; what is the fastest way to parse out the "3" from this string that works in all browsers (back to IE6) More on stackoverflow.com
🌐 stackoverflow.com
🌐
Dirask
dirask.com › posts › JavaScript-get-last-3-characters-of-string-1xVJbp
JavaScript - get last 3 characters of string
// ONLINE-RUNNER:browser; // true means string is empty console.log( "".substring("".length - 3) === "" ); // empty console.log( "1".substring("1".length - 3) ); // 1 console.log( "12".substring("12".length - 3) ); // 12 console.log( "123".substring("123".length - 3) ); // 123 console.log( "1234".substring("1234".length - 3) ); // 234 console.log( "12345".substring("12345".length - 3) ); // 345
🌐
JSchallenger
jschallenger.com › javascript-practice › javascript-fundamentals › get-last-characters-string-javascript
Get last n characters of string | JSchallenger
JSchallenger. Write a function that takes a string as argument. Extract the last 3 characters from the string. Return the result
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-the-last-n-characters-of-a-string-in-javascript
JavaScript - How to Get the Last N Characters of a String? - GeeksforGeeks
July 23, 2025 - The slice() method is the most commonly used approach for extracting the last N characters, thanks to its simplicity and support for negative indices. ... The substring() method calculates the starting index based on the string's length.
🌐
IQCode
iqcode.com › code › javascript › get-last-three-characters-of-string-javascript
get last three characters of string javascript Code Example
March 3, 2022 - t get last character get last indef ox a string in js get last two characters of string in javascript javascript string last 3 characters js check last 3 characters of string last 3 characters of string js javascript last character of string is javascript extract last 4 characters check last letter of string js javascript number get last digit how to remove the first and last charater of a string nodejs javascript substr last 2 characters how to make the last element of a string comes 1st in javascript javascript get last in string return everything but last n characters javascript get last di
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-last-character-of-string
Get last char or last N characters of String in JavaScript | bobbyhadz
Copied!const str = 'bobby'; console.log(str.substring(-3)); // 👉️ bobby console.log(str.slice(-3)); // 👉️ bby · When given a negative index, the slice() method counts backward from the end of the string to find the indexes.
Find elsewhere
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-get-last-n-characters-of-string-4a865e1eed7c
How to Get the Last N Characters of a String in JavaScript | JavaScript in Plain English
July 30, 2024 - const str = 'Coding Beauty';const last3 = str.slice(-3); console.log(last3); // utyconst last6 = str.slice(-6); console.log(last6); // Beautyconst last10 = str.slice(-10); console.log(last10); // ing Beauty · The String() slice() method returns ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-get-the-last-character-of-a-string-in-javascript
JavaScript – How To Get The Last Caracter of a String? | GeeksforGeeks
The second parameter specifies the number of characters to extract. You can directly use the length property of a string to access the last character by treating the string as an array-like object.
Published: November 26, 2024
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Bracket Notation to find the last character in a string - Curriculum Help - The freeCodeCamp Forum
September 1, 2021 - I don’t understand why this code gives a letter a not a number as a result/in the console: var firstName = "Ada"; var lastLetter = firstName[firstName.length - 1]; I question this because this code gives me a number: v…
🌐
Techie Delight
techiedelight.com › home › java › get last n characters of a string in javascript
Get last n characters of a string in JavaScript | Techie Delight
July 7, 2026 - This post will discuss how to get the last `n` characters of a string in JavaScript... There are several methods to get the last `n` characters from a string using JavaScript native methods `substring()` and `slice()`.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-26.php
JavaScript basic: Create a new string from a given string taking the last 3 characters and added at both the front and back - w3resource
JavaScript exercises, practice and solution: Write a JavaScript program to create a string from a given string. This is done by taking the last 3 characters and adding them at both the front and back. The string length must be 3 or more.
🌐
Tutorial Reference
tutorialreference.com › javascript › examples › faq › javascript-how-to-get-the-last-character-or-last-n-characters-of-string
How to Get the Last Character or Last N Characters of a String in JavaScript | Tutorial Reference
The slice() method is the definitive tool for this task. By passing a negative number, you tell it how many characters to extract from the end of the string. Problem: you need to get the last 3 characters of a string.
🌐
Delft Stack
delftstack.com › home › howto › javascript › get last character of a string in javascript
How to Get Last Character of a String in JavaScript | Delft Stack
February 2, 2024 - Hence, we can perform array operations on a string to get its last character. We can use the following inbuilt functions of javascript can be used for this purpose. substr() is similar to the most commonly used function substring(). We can use either of these functions to get a portion of a string based on the parameter values. substr() accepts two parameters, one the index (starting from 0) from where we need to fetch the substring and the length of the substring.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-the-last-character-of-a-string-in-javascript
JavaScript - How To Get The Last Caracter of a String? - GeeksforGeeks
Strings are array-like, so you can use bracket notation to access individual characters. s[s.length - 1] directly retrieves the last character.
Published: July 11, 2025
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-62.php
JavaScript basic: Move last three character to the start of a specified string - w3resource
... // Define a function named ... than 1 if (str.length > 1) { // Use slice to get the last three characters and concatenate with the substring excluding the last three characters return str.slice(-3) + str.slice(0, -3); ...