0

I found a code snippet on this site for parsing cookies. Credit to Pomax for posting it here. There is some syntax in the code that I don't understand:

function parseCookies (headers) {
const list = {};

const { Cookie } = headers;
if (!Cookie) return list;

Cookie.split(`;`).forEach((cookie) => {
    let [ name, ...rest] = cookie.split(`=`);
    name = name?.trim(); //What is this doing?
    if (!name) return;
    const value = rest.join(`=`).trim();
    if (!value) return;
    list[name] = decodeURIComponent(value);
});

return list;

};

The syntax I don't understand is here:

name = name?.trim();

Can someone help me understand what the ? is doing, and possibly point me to where it's documented in the MDN Web Docs? Thank you!

oshox
  • 1
  • 1
  • 1
    basically it lets you try and access something that might not exist without checking that it exists first. you use it when the attribute/method you're accessing is possibly undefined or null. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining – smcrowley Jul 07 '22 at 00:10
  • aside - google is bad at punctuation unless you write out the symbols in english - try `javascript question mark dot`i get https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining #1, ymmv – James Jul 07 '22 at 00:35

0 Answers0