Serialize cookie

OSCC · JavaScript, Browser, String · Oct 22, 2020

Serializes a cookie name-value pair into a Set-Cookie header string.

const serializeCookie = (name, val) =>
  `${encodeURIComponent(name)}=${encodeURIComponent(val)}`;
serializeCookie('foo', 'bar'); // 'foo=bar'

Recommended snippets

  • Parse cookie

    JavaScript, Browser

    Parses an HTTP Cookie header string, returning an object of all cookie name-value pairs.

  • Serialize form

    JavaScript, Browser

    Encodes a set of form elements as a query string.

  • URL parameters as object

    JavaScript, Browser

    Creates an object containing the parameters of the current URL.