Skip to main content

How to get file extension using javascript/jquery

You can get file extension in HTML using javascript/jquery function. You have to pass the filename to the javascript function and this function returns the format of the file. By using this function you can get that the file is an image (png, jpg, jpeg), pdf, or in any other format.

  • Get file extension using file name with replace method in javascript

    <script>
        function getFileExtension(fileName){
            var  fileExtension;
            fileExtension = fileName.replace(/^.*\./, '');
            return fileExtension;
        }
        var fileExt = getFileExtension('filename.jpg');
        console.log(fileExt);
    </script>
    
  • Back to code snippet queries related javascript