How to read a cookie using JavaScript?

Following is the code to read a cookie using JavaScript −

Note − A local server would be needed to run this example.


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-size: 18px;
      font-weight: 500;
      color: blueviolet
<h1>Read a cookie using JavaScript</h1>
<div class="result"></div>
<button class="Btn">Click here</button>
<h3>Click on the above button to read the cookie</h3>
   let BtnEle = document.querySelector(".Btn");
   let resEle = document.querySelector(".result");
   document.cookie = "Name=Rohan Sharma";
   BtnEle.addEventListener("click", () => {
      resEle.innerHTML = document.cookie;


On clicking the ‘CLICK HERE’ button −