JavaScript - What Is Cookies And How To Use It

JAVASCRIPT COOKIES

  • Cookies are the information that is present between the server side and the client side
  • It stores the information of the user on the web pages, in other words, it is the piece of data that is present on the computer

Following things have been included in this blog,

  1. Why do we need them 
  2. How to use/create them
  3. Five Variable fields
  4. What can javascript do with the cookies
  5. Example of how to use them

Why do we need cookies? 

  • Web browser and sever communicate with the help of a protocol HTTP. But the server never stores the information after sending it to the browser. 
  • The information could be required in the future for some important usage 

Here how do cookies come??

  • When we use cookies the browser does not have to communicate with the servers each and every time it needs some data.
  • Instead with the help of cookies the information is stored on the user's computer so that it can be accessible from there.

HOW TO USE COOKIES? 

  • With the help of document.cookie property javascript is able to read write and also create the cookies. 

Syntax of creating a cookie ?? 

?? document.cookie = "name=xyz";

5 Variable fields

With each cookie we can have 5 variable path names or domains to a particular website, based on a list of 5 variable fields

Below are the mentioned variable fields

What javascript can do with the cookies

There are various things javascript can do with the cookies. Some of them are:

  • It can read a cookie
  • It can update a cookie
  • It can delete the cookie
  • It can check the cookie

Let's see some example of how can we create a javascript function in order to check the cookie.