JavaScript handles strings and arrays very well. Sometime its needed to get the last segment or file name of the current page URL and process it for another JavaScript/Ajax request.

Here, I would like to share a fairly easy way.

Let current page URL is http://www.sidd3.com/segment1/segment2/segment3/segment4

Code Example:

Get Last Segment of URL in JavaScript

For the same purpose, JavaScript array function pop() can be used.

Code Example: