[JS] Local Storage , Session Storage

hi·2022년 9월 7일
0
  • key : value 형태
  • 문자, 숫자 데이터 저장 가능

차이점

Local StorageSession Storage
반영구적으로 데이터 저장브라우저를 닫으면 데이터 사라짐

기본 문법

localStorage.setItem('이름', 'kim') 저장
localStorage.getItem('이름') 		출력
localStorage.removeItem('이름') 		삭제

sessionStorage도 동일

array / object 저장

그냥 저장하면 강제로 문자로 바뀌기 때문에 JSON으로 바꿔 저장

JSON.stringify() 저장
JSON.parse() 출력

//저장
var arr = [1,2,3];
var newArr = JSON.stringify(arr);

localStorage.setItem('num', newArr);

//출력
var out = localStorage.getItem('num');
out = JSON.parse(out);
console.log(out);

0개의 댓글