Skip to content

Instantly share code, notes, and snippets.

@joduplessis
Created September 12, 2017 06:50
Show Gist options
  • Save joduplessis/7b3b4340353760e945f972a69e855d11 to your computer and use it in GitHub Desktop.
Save joduplessis/7b3b4340353760e945f972a69e855d11 to your computer and use it in GitHub Desktop.
Setting, deleting and retrieving cookies in Typescript.
@kittanat-mos
Copy link

thank you krub

@AbbottF
Copy link

AbbottF commented May 14, 2022

Thank you. This has helped me weening myself from jQuery Libraries...and morphing my js to ts.

@mctrafik
Copy link

mctrafik commented May 3, 2023

For those seeing this thread in 2023, getting a cookie can be implemented as

const getCookie = (cookieName: string) => new RegExp(`${cookieName}=([^;]+);`).exec(document.cookie)?.[1];

which IMHO is more elegant than shifting values out of an array and splitting out all cookie entries first.

@ayodejii
Copy link

ayodejii commented May 7, 2024

@mctrafik this is good but readability could be better.

@bas-kirill
Copy link

Better to use parts.length === 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment