---
title: "How to Use Local & Cookie Storage using JavaScript modules in Magento 2"
url: "https://magecomp.com/blog/use-local-cookie-storage-using-javascript-modules-in-magento-2/"
date: "2021-03-22T17:31:29+00:00"
modified: "2025-02-20T08:55:22+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 300
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Magento 2 offers the functionality to set and get information for the cookie. Learn How to Use Local & Cookie Storage using JavaScript modules in Magento 2"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to set NOINDEX NOFOLLOW for Specific Page in Magento 2"
    url: "https://magecomp.com/blog/set-noindex-nofollow-for-specific-page-magento-2/"
  - title: "Best Magento 2 Instagram Feed Extensions"
    url: "https://magecomp.com/blog/best-magento-2-instagram-feed-extensions/"
  - title: "7 Things to Know before you Create Magento 2 Migration Plan"
    url: "https://magecomp.com/blog/7-things-to-know-before-you-create-magento-2-migration-plan/"
---

# How to Use Local & Cookie Storage using JavaScript modules in Magento 2

_Published: March 22, 2021_  
_Author: Dhiren Vasoya_  

![How to Use Local &amp; Cookie Storage using JavaScript modules in Magento 2](https://magecomp.com/blog/wp-content/uploads/2021/03/How-to-Use-Local-Cookie-Storage-using-JavaScript-modules-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

Today I am going to explain How to Use Local & Cookie Storage using JavaScript modules in Magento 2.
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

A cookie is a small piece of information stored in a user’s computer when the user is browsing any website via web browsers. After then the user can retrieve those data as and when required. Cookies are generally used to provide a personalized user experience by tracking user’s actions.
Magento 2 provide the functionality to store some public information at browser using local storage or cookie. The store admin can manage the cookie information using javascript modules. Display cookie notification on the store frontend to take consent from the user with [**FREE Cookie Compliance Extension for Magento 2**](https://magecomp.com/magento-2-cookie-compliance.html).
## Steps to Use Local & Cookie Storage using JavaScript modules in Magento 2:
**Step 1:** First Create a JS file in your extension or theme. To do this, create **customcookie.js** in
**app\code\Vendor\Extension\view\Frontend\Web\Js\customcookie.js**

```
define([
'jquery',
'uiComponent',
'jquery/jquery-storageapi'
],
function ($, Component)
{
 	return Component.extend(
        {
		defaults:
                {
			cookieMessages: []
		},
		initialize: function ()
                {
		     this._super();
		     this.cookieMessages = $.cookieStorage.get('mage-string');
		     $.cookieStorage.set('mage-string', '');
		}
	});
});
```

To use this feature, we have to add both jquery and jquery/jquery-storageapi
[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-3.webp)](https://magecomp.com/magento-malware-removal.html)

**Step 2:** So now you can get cookie information using
```
$.cookieStorage.get('mage-string')
```

**Step 3:** window.localStorage is also supported to store the information at the local storage
```
window.localStorage.setItem('myname', 'Vendor');
window.localStorage.getItem('myname');
window.localStorage.removeItem('myname');
window.localStorageclear();
```

These all are different approaches to store information.
## Conclusion:
Hence, with the help of the above code, all are able to. In case you face any difficulty, let me know by mentioning it in the comment section below. Share the article further and stay updated.
You may also like to refer – [**Magento 2 Cookie Notice (Everything You Need to Know)**](https://magecomp.com/blog/how-to-configure-cookie-in-magento-2/)

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-3.webp)](https://magecomp.com/magento-performance-optimization.html)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/use-local-cookie-storage-using-javascript-modules-in-magento-2/](https://magecomp.com/blog/use-local-cookie-storage-using-javascript-modules-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-07-22 21:51:59 UTC_  
