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

๐ŸŒ
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 - You can use a regex pattern to extract the last N characters from a string. This approach is less common but effective in specific use cases. ... const getChars = (s, n) => s.match(new RegExp(`.{${n}}$`))[0]; const s = "Hello World"; console.log(getChars(s, 5)); ... By splitting the string into an array, you can use Array.slice() to extract the last N elements and join them back.
Discussions

jquery - How can I select the last 4 characters of a string using JavaScript? - Stack Overflow
If I understand your question correctly (and I'm not sure I do), you just want to take the last 4 characters of a known string (to translate into decimal, but that wasn't your question). In javascript, you would want to use the substring api. For example, to get the last 4 digits of an arbitrary ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Get last 4 characters of every element of array in JavaScript - Stack Overflow
I would be surprised if this hasn't been asked in the past. I was unable to find something that works. I have an array of strings and I would like to create a new array with only the last 4 charact... More on stackoverflow.com
๐ŸŒ stackoverflow.com
swift - How to get last 4 characters of a string? - Stack Overflow
I need to get the last 4 letters of a string. How can I do that? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Nth-to-Last Character in a String
Tell us whatโ€™s happening: Your code so far // Example var firstName = "Ada"; var thirdToLastLetterOfFirstName = firstName[firstName.length - 3]; // Setup var lastName = "Lovelace"; // Only change code below this line var secondToLastLetterOfLastName = lastName[lastName.lenght -2]; Your browser ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
8
0
July 29, 2019
๐ŸŒ
YouTube
youtube.com โ€บ codemake
javascript get last 4 characters of string - YouTube
Get Free GPT4o with 1 million code snippet from https://codegive.com to get the last 4 characters of a string in javascript, you can use the `substr()` meth...
Published: June 21, 2024
Views: 4
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ get last 4 characters of string javascript | example code
Get last 4 characters of string JavaScript | Example code
June 5, 2024 - <!DOCTYPE html> <html> <body> <script> ... txt.slice(-4); console.log(last4); </script> </body> </html> ... You could also use the String substring method to get the last N characters of a string....
๐ŸŒ
Reactgo
reactgo.com โ€บ home โ€บ how to get last n characters of a string in javascript
How to get last n characters of a string in JavaScript | Reactgo
March 7, 2023 - const name = "Bentley"; const lastFour = name.slice(-4) console.log(lastFour); // "tley" Similarly, we can also get the last n characters of a string by using the substring() method.
๐ŸŒ
HatchJS
hatchjs.com โ€บ home โ€บ javascript: get the last 4 characters of a string
JavaScript: Get the Last 4 Characters of a String
January 5, 2024 - A: There are a few ways to get the last 4 characters of a string in JavaScript. One way is to use the `slice()` method. The `slice()` method takes two arguments: the start index and the end index.
Find elsewhere
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ javascript-get-last-n-characters-of-string
How to get the last N characters of a string in JavaScript
October 25, 2022 - The substring() method works similarly to slice() and returns a new string containing part of the original string specified using start and end indexes. However, unlike slice(), the substring() method uses 0 as a start index if a negative number ...
๐ŸŒ
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 last6 = str.slice(-6); console.log(last6); // Beautyconst last6Again = str.slice(str.length - 6); console.log(last6Again); // Beauty ... Available for free exclusively on the free and open blogging platform, Differ. ... If we try to get more characters than the string contains, slice() returns the entire string instead of throwing an error.
๐ŸŒ
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
By splitting the string into an array, you can use the pop() method to retrieve and remove the last character. ... Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn ...
Published: November 26, 2024
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-get-last-character-of-string
Get last char or last N characters of String in JavaScript | bobbyhadz
To get the last character of a string, call the `charAt()` method on the string, passing it the last index as a parameter.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-get-the-last-n-characters-of-a-string-in-javascript
JavaScript โ€“ How to Get the Last N Characters of a String? | GeeksforGeeks
November 26, 2024 - In this article, we are going to learn about Lexicographically next string in JavaScript. A lexicographically next string is the immediate string in a sorted order that comes after a given string when considering character sequences. It's determined by rearranging characters to find the smallest cha ... Here are the various approaches to get the last character of a String using JavaScript.1.
๐ŸŒ
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()`.
๐ŸŒ
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 - The slice() function is also a commonly used method to operate on strings. It is similar to the substring() function for the arguments accepted by it. Both the functions accept two parameters, the start index and the end index.
๐ŸŒ
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
By passing -1 as an argument, you can extract the last character. ... A negative index in slice() counts from the end of the string. ... The substr() method extracts a substring starting from a specified position.
Published: July 11, 2025
๐ŸŒ
Medium
medium.com โ€บ @kanmitowolabi โ€บ how-to-get-the-first-n-characters-and-last-n-characters-of-a-string-in-javascript-e9e79dc92c90
How to Get the First N Characters and Last N Characters of a String in JavaScript - Kanmit Owolabi - Medium
February 7, 2024 - To get the last N characters of a String, using slice(): export const GET_LAST_N_DIGITS = (str: any, n: number) => { return str.slice(str.length - n); } console.log(GET_LAST_N_DIGITS("531667272871819", 4)) // 1819
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ substring
String.prototype.substring() - JavaScript - MDN Web Docs
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied. const str = "Mozilla"; console.log(str.substring(1, 3)); // Expected output: "oz" console.log(str.substring(2)); ...
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ curriculum help
Nth-to-Last Character in a String - Curriculum Help - The freeCodeCamp Forum
July 29, 2019 - Tell us whatโ€™s happening: Your code so far // Example var firstName = "Ada"; var thirdToLastLetterOfFirstName = firstName[firstName.length - 3]; // Setup var lastName = "Lovelace"; // Only change code below this line var secondToLastLetterOfLastName = lastName[lastName.lenght -2]; Your browser ...