Featured Article

Master W3Schools JS Cookies: Quick Guide To Set Get And Delete

Adam Jul 13, 2026

Dive into the world of web development with us today as we explore JavaScript cookies – a fundamental aspect of client-side web storage. Widely used to maintain stateful information across a single session, cookies enhance user experience and facilitate features like login authentication, session management, and personalized settings. Let's delve into the intricacies of JavaScript cookies, guided by the clear and concise tutorials provided by W3Schools.

a poster showing the different types of cookies and what they are labeled in each cookie
a poster showing the different types of cookies and what they are labeled in each cookie

Before we embark on our learning journey, let's briefly understand what cookies are and their significance. Cookies are small text files stored on the user's device by the web browser. They contain key-value pairs, with keys serving as names and values storing the actual data. These tiny powerhouses enable web applications to recall and manipulate data, even as users navigate between pages.

Cookie Design Flavor Ideas | Baking
Cookie Design Flavor Ideas | Baking

Setting Cookies with JavaScript

W3Schools offers a simple and intuitive approach to setting cookies using JavaScript. The `document.cookie` property allows us to create, read, and modify cookies directly in our code.

Chicago Public School Butter Cookies
Chicago Public School Butter Cookies

To set a cookie, we use the following syntax: `document.cookie = "key=value";`. Here's a basic example from W3Schools:

document.cookie = "username=John Doe";

Assigning Cookie Expiration Date

Must-Have Back to School Cookies: 15+ Recipes to Try This Year
Must-Have Back to School Cookies: 15+ Recipes to Try This Year

By default, cookies expire when the browsing session ends. To persist cookies across sessions, we assign an expiration date. The `expires` attribute accepts a date as a string in the format `"YYYY-MM-DD HH:MM:SS"`. Here's how you can set an expiration date:

let d = new Date();
d.setTime(d.getTime() + (30*24*60*60*1000));
document.cookie = "username=John Doe; expires=" + d.toUTCString();

In the above example, the cookie will expire in 30 days.

Assigning Cookie Domain and Path

Creative 19 Back to School Cookie Ideas for You
Creative 19 Back to School Cookie Ideas for You

Cookies can be set to apply only to a specific domain or path. The `domain` and `path` attributes allow us to define these constraints:

document.cookie = "username=John Doe; domain=.w3schools.com; path=/";

The cookie above will only be accessible within the 'w3schools.com' domain and its subdomains, like 'www.w3schools.com'.

Reading Cookies with JavaScript

Easy Cookie Recipes for Back to School
Easy Cookie Recipes for Back to School

Now that we know how to set cookies let's explore how to read and retrieve their values. The `document.cookie` property returns a string containing all cookies stored for a particular domain. This string is in the format 'key=value; key2=value2; key3=value3;', making it easy to parse and extract individual cookies.

Parsing Cookie String

COSMIC BROWNIE COOKIES 😍
COSMIC BROWNIE COOKIES 😍
15 Best Back to School Cookie Recipes
15 Best Back to School Cookie Recipes
🍪 88 Must-Try Back-to-School Cookies for 2024
🍪 88 Must-Try Back-to-School Cookies for 2024
some cookies that are on top of a cookie sheet with the words back to school written on them
some cookies that are on top of a cookie sheet with the words back to school written on them
Bake Fun Pineapple Upside-Down Cookies: A Tasty Treat for Kids and Summer
Bake Fun Pineapple Upside-Down Cookies: A Tasty Treat for Kids and Summer
Back to School Royal Icing Sugar Cookies | Foodal
Back to School Royal Icing Sugar Cookies | Foodal
Perfect Back to School Sugar Cookies – Cute and Tasty Ideas
Perfect Back to School Sugar Cookies – Cute and Tasty Ideas
w3schools js cookies
w3schools js cookies
an open box filled with cookies decorated like school supplies
an open box filled with cookies decorated like school supplies
Ultimate Super Thick Mini M&M Chocolate Chip Cookies
Ultimate Super Thick Mini M&M Chocolate Chip Cookies
Irresistibly Warm Chewy Peanut Butter Cookies You Will Love
Irresistibly Warm Chewy Peanut Butter Cookies You Will Love
Copycat Subway Cookies
Copycat Subway Cookies
\
\
Peanut Butter and Jelly Sandwich Cookies for Creative Kid Treats
Peanut Butter and Jelly Sandwich Cookies for Creative Kid Treats
Swirled Brownie Cookies With Peanut Butter For A Unique Treat
Swirled Brownie Cookies With Peanut Butter For A Unique Treat
40 Easy Cookie Decorating Ideas
40 Easy Cookie Decorating Ideas
cookies are cooling on a rack with chocolate sauce in the middle and one cookie is half eaten
cookies are cooling on a rack with chocolate sauce in the middle and one cookie is half eaten
decorated cookies are arranged in the shape of an apple, pencils, and ruler
decorated cookies are arranged in the shape of an apple, pencils, and ruler
an image of cookies with chocolate chips on top and the title overlay reads copycat recipe for chick - fil - a cookies
an image of cookies with chocolate chips on top and the title overlay reads copycat recipe for chick - fil - a cookies
decorated cookies with thank you for a great year written on them
decorated cookies with thank you for a great year written on them

To extract individual cookies, we can use the `Document cookie` property combined with the `split()`, `pop()`, and `split('=')` methods. Here's a simple function that retrieves all cookies and their values:

function getCookies() {
    let cookies = document.cookie.split(';');

    for (let i = 0; i < cookies.length; i++) {
        let cookie = cookies[i].split('=');
        console.log(cookie[0].trim() + ' = ' + cookie[1]);
    }
}

The `getCookies()` function will log all cookies and their respective values to the console.

Retrieving a Specific Cookie

To retrieve a specific cookie, we search for the target key within the cookie string. W3Schools provides an example function `getCookie("keyname")` to achieve this:

function getCookie(cname) {
    let name = cname + "=";
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.split(';');
    for(let i = 0; i 

This function returns the value of the specified cookie, or an empty string if the cookie doesn't exist.

Deleting Cookies with JavaScript

To delete a cookie, we set its expiration date to a past date. This essentially 'expires' the cookie, removing it from the user's device.

Deleting a Specific Cookie

Here's how to delete a cookie with a given name, using W3Schools' example function:

function deleteCookie(cname) {
    document.cookie = cname + '=; max-age=0';
}

The `deleteCookie('keyname')` method sets the cookie's expiration date to the past, effectively deleting it.

This exploration of JavaScript cookies has equipped us with the essential knowledge and tools to manipulate cookies in our web applications. Utilizing W3Schools' clear and concise tutorials, we'veLearn how to set, read, and delete cookies, and even assigned expiration dates, domains, and paths. Embrace these newfound skills to enhance user experience and functionality in your projects.