Skip to content

getExtensionName

The getExtensionName() function returns the last extension of a path, with or without the leading dot.

WARNING

Works only with POSIX paths (not Windows paths).

Interactive example

Syntax

typescript
function getExtensionName<
	GenericPath extends string
>(
	path: GenericPath,
	params?: {
		withDot?: boolean;
	}
): string | null;

Parameters

  • path : The path to analyze.
  • params : Optional behavior options.
  • params.withDot : When true, keeps the leading dot in the returned extension.

Return value

The last extension segment:

  • without the dot by default (for example txt)
  • with the dot when withDot is true (for example .txt)
  • null when no extension is found

See also

Released under the MIT license.