Get File Extension From Any URL Or Path using Javascript

In this article, I have explained how can we get file extension In javascript.

Many time we need file extension while doing any task.
Using split() and then taking the final item from the array were the first ideas that sprang to mind.
But I like to do it in a single line. I first learned about pop(), which returns the array’s final element, at that time.

url = http://xyz.com/blob/anyfile.pdf
filePath= c:\demofolder\demofile.txt
ext1 = url.split(".").pop()
ext2 = filePath.split(".").pop()

The pop() function returns the element that was removed last from an array. This technique modifies the array’s length.

The final element in a given array is removed (or “popped”) using the pop() function. Through this removal operation, the technique modifies the original array itself. The procedure then gives us back the deleted element.

This approach is meant to be general. This is to guarantee that objects that resemble arrays can use or call this function.
In an empty array, pop() returns undefined when called.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories