Javascript Ls/ss/cookies😎
Tags: javascript, localstorage, cookies
Published: 2024-06-27 14:10:51

**Browser Memory:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ez10mrbeuo3jsn6qfz9.png)

- localStorage
- Session Storage
- Cookies

**Method**

SetItem();
GetItem();
RemoveItem();
Clear();


**Local Storage**
localStorage foydalanuvchi brauzerida ma'lumotlarni uzoq muddat saqlash uchun ishlatiladi. Saqlangan ma'lumotlar brauzer yopilgandan keyin ham saqlanadi.localStorage odatda har bir kelib chiqadigan domen uchun taxminan 5-10 MB xotira ajratiladi. Bu miqdor brauzer va qurilmaga qarab biroz farq qilishi mumkin.

Ma'lumot saqlash(SetItem):

```
localStorage.setItem('kalit', 'qiymat');

```

cancole:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zl2jtklq0xjkq2m1qf6.png)

Ma'lumot olish(GetItem):

```
let qiymat = localStorage.getItem('kalit');
cansole.log(qiymat)
```
cansole:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8toghysty3zen0vf80mp.png)

Ma'lumotni o'chirish(removeItem):

```
localStorage.removeItem('kalit');

```

Barcha ma'lumotlarni o'chirish(Clear):

```
localStorage.clear();

```

**Session Storage**
sessionStorage ham foydalanuvchi brauzerida ma'lumotlarni saqlash uchun ishlatiladi, lekin bu ma'lumotlar faqat sessiya davomida saqlanadi. Ya'ni, brauzer oynasi yopilganda ma'lumotlar o'chiriladi.sessionStorage ham odatda har bir kelib chiqadigan domen uchun 5-10 MB xotira ajratiladi. Bu miqdor ham brauzer va qurilmaga qarab o'zgarishi mumkin. sessionStorage faqat sessiya davomida ma'lumotlarni saqlaydi va sessiya tugashi bilan (brauzer oynasi yopilganda) ma'lumotlar o'chiriladi.

Ma'lumot saqlash:

```
sessionStorage.setItem('kalit', 'qiymat');

```
Ma'lumot olish:

```
let qiymat = sessionStorage.getItem('kalit');

```
Ma'lumotni o'chirish:

```
sessionStorage.removeItem('kalit');

```
Barcha ma'lumotlarni o'chirish:

```
sessionStorage.clear();

```



**Cookies**
Cookies kichik ma'lumotlar bo'lib, ular brauzerda saqlanadi va veb-saytlar tomonidan o'qilishi mumkin. Cookies maxsus muddat bilan belgilanishi mumkin va brauzer yopilganda yoki maxsus vaqt oralig'ida o'chirilishi mumkin.

Cookie saqlash:

```
document.cookie = "kalit=qiymat; path=/; max-age=3600"; // 1 soat davomida saqlanadi

```
Cookie olish:

```
function getCookie(kalit) {
  let name = kalit + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i < ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

```
Cookie o'chirish:

```
document.cookie = "kalit=; path=/; max-age=0";

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19yapc2724ly51tw54a0.png)

Xotira hajmi brauzer va platformaga qarab biroz farq qilishi mumkin. Ba'zi mashhur brauzerlarning localStorage va sessionStorage hajmlari haqida umumiy ma'lumot:

- Google Chrome: Taxminan 10 MB.
- Mozilla Firefox: Taxminan 10 MB.
- Microsoft Edge: Taxminan 10 MB.
- Safari: Taxminan 5 MB.
- Opera: Taxminan 10 MB.