How can I use JavaScript to get a cookie by name and extract its values into an array?

Been using JavaScript in frontend-heavy projects for around 6 years — and I really like cleaner, more modern syntax.

Building on what Rima said, if you want something a bit more modern and elegant, using Array.prototype.find() makes the javascript get cookie by name task much cleaner:

function getCookieByName(name) {
    const cookie = document.cookie.split('; ').find(cookie => cookie.startsWith(name + '='));
    return cookie ? cookie.split('=')[1].split(',') : [];
}

const obligations = getCookieByName('obligations');
console.log(obligations);

Instead of looping manually, find() helps us directly locate the cookie we need. Then we just split its value. This method feels a lot more readable, and when working on teams, clean code like this saves everyone time. Personally, I love this when I’m working on codebases that prioritize modern JavaScript practices.