How to get File Extension using JavaScript

Today you will learn how to get file extension from given file name using JavaScript. Here we will see two different methods to get the file extension. You may need it during File Upload or Preview Image.

Way to get file extension using JavaScript

  1. Using split() and pop() method
  2. Using substring() and lastIndexOf() method

1. Using split() and pop() method

Let’s use the split() and pop() methods to split the string and get the last element.

2. Using substring() and lastIndexOf() method

In this second method, we’ll use the substring() and lastIndexOf() to get the file extension.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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