[Home] > Snippets  > Languages  > JavaScript  > Misc  >  Get the value of a param from a URL

Get the value of a param from a URL

JavaScript

const getParam = (url, param) => new URLSearchParams(new URL(url).search).get(param)

TypeScript

const getParam = (url: string, param: string): string | null =>
new URLSearchParams(new URL(url).search).get(param)

Examples

getParam('http://domain.com?message=hello', 'message') // 'hello'