Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback


Deno standard library
Go to Latest
function basename
import { basename } from "";

Return the last portion of a URL, or the host name if there is no path. Trailing /s are ignored, and optional suffix is removed.


Example 1

import { basename } from "";

// basename accepts a string or URL
console.log(basename(""));  // "mod.ts"
console.log(basename(new URL(""))); // "mod.ts"

// basename accepts an optional suffix to remove
console.log(basename(new URL(""), ".ts")); // "mod"

// basename does not include query parameters or hash fragments
console.log(basename(new URL(""))); // "mod.ts"
console.log(basename(new URL(""))); // "mod.ts"

// If no path is present, the host name is returned
console.log(basename(new URL(""))); // ""


url: string | URL
  • url to extract the final path segment from.
suffix: string
  • optional suffix to remove from extracted name.



the last portion of the URL path, or the URL origin if there is no path.