If you’ve worked with String data type and url strings, you are probably no stranger to get the value of a string after last slash using JavaScript. This article will show you the 3 most straightforward ways to do it.
Get the value of a string after last slash using JavaScript
Here are three ways to get the value of a String after last Slash using JavaScript. Read on, and don’t forget to practice following the examples.
Using the substring()
method and lastIndexOf()
method
We’ve already introduced the syntax of the substring()
method here. You can read more about it if you want.
The substring()
method will extract the string from a given string. The starting position of the substring is specified in the 1st argument, and the length of the string is specified in the 2nd argument.
The lastIndexOf()
method returns the index in the calling string object of the last occurrence of the given value, starting the search at fromIndex. It returns -1 if the value is not found.
lastIndexOf()
syntax:
string.lastIndexOf(searchValue, [fromIndex])
Parameters:
- searchValue: a string representing the value to search for.
- fromIndex (Optional): the position in the calling string to start the search. It can be any integer value between 0 and the string length. The default value is 0.
First, we will use the lastIndexOf()
function to find the last position of the /
character in the string. We then pass the last position of the /
character we just found plus 1 to inside the substring()
function to extract the value after the last /
from the original string and get the result as a new string. See the code below for a better understanding.
const url = "https://learnshareit.com/category/javascript";
// Using lastIndexOf()
const lastIndex = url.lastIndexOf('/');
// Using substring()
const result = url.substring(lastIndex + 1);
console.log(result)
Output:
javascript
Using the split()
method
We introduced the syntax of split()
in a previous post. You can see more here.
First, we will use the split()
function to split by the ‘/
‘ position and create an array of the substrings. Then we need to access the last element of that array to get the last string after the ‘/
‘ character. See the code below for a better understanding.
const url = "https://learnshareit.com/category/javascript";
// split string by /
const urlArray = url.split("/");
const result = urlArray[urlArray.length - 1];
console.log(result)
Output:
javascript
Using the split()
method and pop()
method
We can read the syntax of split()
in this post.
The pop()
method in JavaScript is used to remove the last element in the array and return the deleted element.
pop()
syntax:
array.pop()
Parameters:
- array: array to remove the element.
First, we will use the split()
function to split the string by the ‘/’ position. Then we use the pop()
function to delete the last element of the newly created array and return that last element. See the code below for a better understanding.
const url = "https://learnshareit.com/category/javascript";
// split string by /
const urlArray = url.split("/");
// Using pop()
const result = urlArray.pop();
console.log(result)
Output:
javascript
Summary
The above article showed you how to get the value of a string after last slash using JavaScript. The lastIndexOf()
method and substring()
method is probably the most efficient. We hope the article will be useful to you.
Have a great day!
Maybe you are interested:
- Remove The Leading And Trailing Comma From A String Using JavaScript
- How to Replace Spaces with Underscores in JavaScript
- How To Get The First 3 Characters Of A String In JavaScript?

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java