Skip to content

Instantly share code, notes, and snippets.

@Ugarz
Last active August 2, 2017 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ugarz/361ea2fa4ce6e97a6d1c741190d59229 to your computer and use it in GitHub Desktop.
Save Ugarz/361ea2fa4ce6e97a6d1c741190d59229 to your computer and use it in GitHub Desktop.
Get a file extension with javascript (no regex)

Get a file extension simply, no regex

Full credits to this thread

const getFileExtension = (filename) => {
  const splitedName = filename.split('.');
  console.log(splitedName) // [ 'fileblabla', 'pdf' ]
  if (splitedName.length === 1 || (splitedName[0] === '' && splitedName.length === 2)) {
    return '';
  }
  return splitedName.pop();
};

const result = getFileExtension('fileblabla.pdf')
console.log(result) // pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment