Following is the code to read a cookie using JavaScript −
Note − A local server would be needed to run this example.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.result {
font-size: 18px;
font-weight: 500;
color: blueviolet
}
</style>
</head>
<body>
<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>
<script>
let BtnEle = document.querySelector(".Btn");
let resEle = document.querySelector(".result");
document.cookie = "Name=Rohan Sharma";
BtnEle.addEventListener("click", () => {
resEle.innerHTML = document.cookie;
});
</script>
</body>
</html>Output

On clicking the ‘CLICK HERE’ button −
