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

Discussions

javascript - How to get the last character of a string? - Stack Overflow
A negative start index slices the ... -1, the last character is extracted: ... Christian C. Salvadó · 832k185185 gold badges929929 silver badges846846 bronze badges ... Side note: arrays also have a slice() method. - Their functionality is conceptually similar (partial copies) -------- (Just in case you're reading code and see .slice()) 2010-10-07T19:08:54.967Z+00:00 ... What do you mean by a "UTF-8 string"? The phrase makes no sense. FWIW though, JavaScript strings are ... 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
javascript - How do I get the n last letters in a string - Stack Overflow
I have a bunch of strings in javascript which hold dates such as "2015-02-17", how can I get the n last characters of that string as a substring like "02-17" or "17" ? More on stackoverflow.com
🌐 stackoverflow.com
How do I get the last two characters in a string?
Well, "character" isn't well defined in unicode, so it depends. But String::pop() returns a char, which is a unicode scalar value, so we'll go with that :) Easiest way is to use an iterator: let s = String::from("hello world"); let last_two: Vec = s.chars().rev().take(2).collect(); println!("{:?}", last_two); Take the chars, reverse it, then take two. More on reddit.com
🌐 r/rust
15
8
November 16, 2015
🌐
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()`.
🌐
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 - New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers. ... To get the last N characters of a string in JavaScript, call the slice() method on the string, passing -N as an argument.
🌐
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 - To access the last n characters of a string in JavaScript, we can use the built-in slice() method by passing -n as an argument to it.
🌐
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 - Input : $str = "GeeksforGeeks!" $n = 9 Output : forGeeks! Method 1: Tn this method, traverse the last N characters of the string and keep appending them in a new string. E ... Here are the various methods to find the first and last character of a string in JavaScript.1.
🌐
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 - You can use the slice() method to get the last N characters of a string in JavaScript, passing -n as a negative start index.
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to get the last n characters of a string in javascript
How to Get the Last N Characters of a String in JavaScript - Coding Beauty
June 24, 2022 - To get the last N characters of a string in JavaScript, call the slice() method on the string, passing -N as an argument.
Find elsewhere
🌐
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
🌐
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
This guide will teach you the best methods for getting the last character and the last N characters from a string, explaining the use case for each and why they are superior to older approaches. There are two excellent, modern methods for this. The String.prototype.at() method is the newest ...
🌐
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
Here are some of the most common ... a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s ... Here are the various methods to get character array from a string in JavaScript.1. Using String split() MethodThe split() ...
Published: November 26, 2024
🌐
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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript - MDN Web Docs
The following example uses the substring() method and length property to extract the last characters of a particular string. This method may be easier to remember, given that you don't need to know the starting and ending indices as you would in the above examples.
🌐
Stack Abuse
stackabuse.com › bytes › trim-the-last-n-characters-from-a-string-in-javascript
Trim the Last N Characters from a String in JavaScript
August 21, 2023 - We learned how to use the String.substring() method, and we also looked at how to conditionally remove characters. Follow to keep up with AI and the future of software · Practical takes on AI, tooling, and where development is headed. ... Reliable monitoring for your app, databases, ...
🌐
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 ...