How to Store Data In Cookies Using JavaScript

Store Data In Cookies Using JavaScript, before we start setting the cookie using JavaScript, first we should discuss what are Cookies in JS. And then we start all the cookie operations.

On this complete article we majorly discuss about,

  1. What are the Cookies?
  2. JavaScript Cookies Operations
  3. Store Data In Cookies using JavaScript (Set Cookie) with example
  4. JavaScript Get Cookie By Name with example
  5. JavaScript Get all Cookies with example
  6. Delete Cookie in JavaScript with example
How to Store Data In Cookies Using JavaScript
Complete Guide about JavaScript Cookies

What are Cookies?

Cookies are the set of information that is stored on the user’s system or browser.

Cookies were invented to solve the problem of “how to remember information about the user”,

  • When any user visits any page of the website.
  • Cookies are remember their details by cookie.

JavaScript Cookies Operations

  1. Set-Cookie in JS
  2. JavaScript Get Cookie by name
  3. JavaScript Get all Cookies
  4. Delete Cookie JavaScript

Store Data In Cookies using JavaScript (Set Cookie)

For storing user data on cookie using JavaScript is we use document.cookie syntax. Generally we can set 3 parameters to the document.cookie,

  • Any name or other information like “username=John Doe”
  • The expiry time of the cookie from user’s system.
  • Path of the cookies on the same.

Syntax and Example of Set Cookie using JS

Above code is also example of storing cookies in JavaScript.

JavaScript Get Cookie By Name

Generally, developers are struggling with getting the value of the cookie by its name. We will give you a very basic example of JavaScript Get Cookie By Name.

Note: Copy the below code and paste it on the browser’s developer tool console and hit enter to see the output.

On the same code, we use var readallCookie = document.cookie for getting all the cookies.

Example of Get Cookie By Name JavaScript

Output:

JavaScript Get Cookie by name
JavaScript Get Cookie by name

JavaScript Get all Cookies

For getting all the cookies using JavaScript we use the var readallCookie = document.cookie.

Note: Copy the below code and paste it to your console and hit enter to check the complete list of cookies.

Output:

JavaScript Get all Cookies
JavaScript Get all Cookies

Delete Cookie JavaScript

For deletion of cookie in JS you have to follow 2 major steps,

  • Set Cookie value empty.
  • And expiry parameter set to passed date.

Example of Delete Cookie JavaScript

Here username is cookie value and the expires have passed date.

Above are the complete solution for the JavaScript Cookies. If you have any issue with cookies you can comment below we love to help.

Check in-depth https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

Also Check:

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

4 thoughts on “How to Store Data In Cookies Using JavaScript”

Leave a Reply

Your email address will not be published. Required fields are marked *