Determine File Extensions Using JavaScript

A simple PHP function to determine a file’s extension.

fileExt()

function fileExt(path) {
    return path.substr(path.lastIndexOf('.') + 1);
}

Sample Output

Input Output()
file.ext ext
/folder/subfolder/file.ext ext
file.bak.ext ext
If you enjoyed this article, please share it with a friend!

2 Responses to Determine File Extensions Using JavaScript

  1. Arif Uddin says:

    genious man… just when i thought reversing the string then find the first occurrence of ‘.’ … i thought search in google… and i get what i need…

    thanks again.. for publishing this little code…

  2. That’s a good script but here’s another one. It takes some people a while to understand that substr function so I wrote another one for the light heads. (I was a light head too so I know how it is)